2012-03-10 65 views
0

我使用Visual Studio 2008創建Windows應用程序。我想以斷開的方式創建登錄表單。我面臨的一個問題是,當我試圖檢查DataTable是否被填充時,它表明它始終被填充。c中斷開連接體系結構的登錄表單#

的登錄按鈕代碼:

try 
     { 
      //getting connectionString 
      mcc = new myConnectionClass(); 
      conString = mcc.MyConnection; 
      //opening connection con 
      con = new SqlConnection(conString); 
      string queryString = "select * from LOGIN_TABLE where LOGIN_ID = @LOGIN_ID and LOGIN_PASSWORD = @LOGIN_PASSWORD"; 
      //dataset ds , SqlDataAdapter sda 
      ds = new DataSet(); 
      sda = new SqlDataAdapter(); 
      //sqlcommand 
      cmd = new SqlCommand(queryString, con); 
      cmd.Parameters.Add(new SqlParameter("LOGIN_ID", txtLogin.Text)); 
      cmd.Parameters.Add(new SqlParameter("LOGIN_PASSWORD", txtPass.Text)); 
      sda.SelectCommand = cmd; 

      sda.Fill(ds, "LOGIN_TABLE"); 
      //datatable dt 
      dt = ds.Tables["LOGIN_TABLE"]; 
      //checking is datatable is empty 
      if (dt != null) 
      { 
       MessageBox.Show("Login Successed"); 
      } 
      else 
      {     
       MessageBox.Show("Login Failed"); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      ds.Clear(); 
      dt.Clear(); 
     } 

回答

1

你的錯誤就在於檢查

if(dt != null) 

如果您LOGIN_TABLE存在

你需要檢查的數量是永諾真行。

if(dt.Rows.Count != 0) 

讓我說也認爲,從安全的角度來看,數據庫存儲上登錄電子郵件密碼是不是一個好主意。

+0

嘿感謝它正在 – 2012-03-10 17:44:33

+0

u能告訴我,如果東西除了可以做,以提高該代碼 – 2012-03-10 17:45:26

+1

使用在你的SqlConnection,以確保正確的關閉,該資源 – Steve 2012-03-10 17:48:12