简体   繁体   中英

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll In VS2019

Im a beginner, and new to C#, i don't know how to fix this bug, please help SQL Service is running, VS2019 is updated to newest and i am Windows 11. It only said Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll Code:

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);
                }
            } 

Change your catch block to

    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());
    }

It will give you a descriptive error message which is usually self explanatory.

Reference : https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlexception?view=dotnet-plat-ext-5.0

SQL 对我来说太难了,可能我会去 .xlsx,是的,我知道它非常不安全,但我退出了,无论如何,谢谢

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM