簡體   English   中英

SqlException未處理

[英]SqlException was Unhandled

我正在嘗試用數據庫在C#中創建登錄表單,我不知道該怎么辦,這是代碼錯誤,說sda.Fill(dt)部分未處理SQLException,這是代碼

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

替換sda.Fill(dt);

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

並編輯您的問題以包括新的輸出。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM