抛出异常:'System.Data.SqlClient.SqlException' in System.Data.dll In VS2019
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll In VS2019
我是初学者,刚接触C#,我不知道如何修复这个错误,请帮忙
SQL服务是运行,VS2019更新到最新,我是Windows11。
它只说异常抛出:'System.Data.SqlClient.SqlException' in System.Data.dll
代码:
string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LoginDB.mdf;Integrated Security=True;";
private void Loginbutton_Click(object sender, EventArgs e)
{
if (LoginTextBox.Text == "" || PassTextBox.Text == "") // Nếu 1 trong 2 ô trống thì
{
MessageBox.Show("Missing login name or password");
return;
}
try
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from LoginDB where UserName=@username and Password=@password", con);
cmd.Parameters.AddWithValue("@username", LoginTextBox.Text);
cmd.Parameters.AddWithValue("@password", PassTextBox.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
//If count =1 thì hiện f2(Menu)
if (count == 1)
{
MessageBox.Show("Login Successfully!");
this.Visible = false;
Menu f2 = new Menu();
f2.Show();
}
else
{
MessageBox.Show("Login Name or password is wrong");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
将您的捕获块更改为
catch (SqlException ex)
{
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"LineNumber: " + ex.Errors[i].LineNumber + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Procedure: " + ex.Errors[i].Procedure + "\n");
}
MessageBox.Show(errorMessages.ToString());
}
它会给你一个描述性的错误消息,通常是不言自明的。
SQL 对我来说太难了,也许我会去 .xlsx,是的,我知道它很不安全,但我放弃了,无论如何谢谢
我是初学者,刚接触C#,我不知道如何修复这个错误,请帮忙 SQL服务是运行,VS2019更新到最新,我是Windows11。 它只说异常抛出:'System.Data.SqlClient.SqlException' in System.Data.dll 代码:
string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LoginDB.mdf;Integrated Security=True;";
private void Loginbutton_Click(object sender, EventArgs e)
{
if (LoginTextBox.Text == "" || PassTextBox.Text == "") // Nếu 1 trong 2 ô trống thì
{
MessageBox.Show("Missing login name or password");
return;
}
try
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from LoginDB where UserName=@username and Password=@password", con);
cmd.Parameters.AddWithValue("@username", LoginTextBox.Text);
cmd.Parameters.AddWithValue("@password", PassTextBox.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
//If count =1 thì hiện f2(Menu)
if (count == 1)
{
MessageBox.Show("Login Successfully!");
this.Visible = false;
Menu f2 = new Menu();
f2.Show();
}
else
{
MessageBox.Show("Login Name or password is wrong");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
将您的捕获块更改为
catch (SqlException ex)
{
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"LineNumber: " + ex.Errors[i].LineNumber + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Procedure: " + ex.Errors[i].Procedure + "\n");
}
MessageBox.Show(errorMessages.ToString());
}
它会给你一个描述性的错误消息,通常是不言自明的。
SQL 对我来说太难了,也许我会去 .xlsx,是的,我知道它很不安全,但我放弃了,无论如何谢谢