SqlException未处理

时间:2017-02-12 12:29:19

标签: c# sql-server

我试图在c#中使用数据库进行登录表单我不知道该怎么做,这是错误所说的SQLException在sda.Fill(dt)部分未处理的代码)这是代码

        SqlConnection con = new SqlConnection(@"Data Source=.\LOUI;Initial Catalog=login_db;User ID=sa;Password=1029384756");
        SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From login_tbl where username = '" + User_txt.Text + "'and password = '" +Pass_txt.Text+ "'",con);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        if (dt.Rows[0][0].ToString() == "1")
        {

            this.Hide();
            adminpanel ap = new adminpanel();
            ap.Show();

        }
        else
        {
            MessageBox.Show("Check Username or Password");
        }

2 个答案:

答案 0 :(得分:2)

sda.Fill(dt);替换为

try
{
    sda.Fill(dt);
}
catch (SQLException ex)
{
    Console.WriteLine(ex.ToString());
}

并编辑您的问题以包含新输出。

答案 1 :(得分:0)

try
{

    SqlConnection con = new SqlConnection(@"Data Source=.\LOUI;InitialCatalog=login_db;User ID=sa;Password=1029384756");//problem is here

    SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From login where name = '" + User_txt.Text + "'and pass = '" + Pass_txt.Text + "'", con); 

    DataTable dt = new DataTable();
    sda.Fill(dt); 

    if (dt.Rows[0][0].ToString() == "1")
    {
        this.Hide();
        adminpanel ap = new adminpanel();
        ap.Show();
    }
    else
    {
        MessageBox.Show("Check Username or Password");
    }
}
catch (Exception z)
{
   MessageBox.Show("Connection error");
}