2017-02-12 88 views
1

我想在C#中的数据库与窗体中的日志我不知道该怎么做,它是代码错误说SQLException是未处理的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"); 
     } 
+4

将代码包装在try/catch块中并处理异常。例外是告诉你问题是什么。此外,请注意,您的代码是*全面打开*到SQL注入**。从安全角度来看,这意味着用户可以在数据库上执行任意代码。从调试的角度来看,这意味着我们甚至不知道您在这里执行的SQL代码。 – David

回答

2

更换sda.Fill(dt);

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

和编辑您的问题,包括新的输出。

+0

谢谢你,我会试试 – jlouie

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

请修复格式并添加关于更改的说明。此外,这不是一个真正的答案。 –

+0

@PeterBons我只是修改了格式。只需等待批准:) – Benni

+0

这和我的回复完全一样。 – Benni