2015-06-21 42 views
-1

我有一個文本框,它將從用戶接收輸入並搜索插入的數據是否在SQL數據庫表中可用。如果數據在表中,那麼它將更新同一行的兩列(time_out和day_out)。
否則它會顯示一條錯誤消息。以下代碼無法使用。請幫忙。從文本框中檢查數據庫數據

try 
{ 

    SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True"); 
    con3.Open(); 
    SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE [email protected]",con3); 
    cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text); 

    SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET [email protected],[email protected] WHERE [email protected]", con3); 
    cmd3.Parameters.AddWithValue("@id", 1); 
    cmd3.Parameters.AddWithValue("@dO", DateTime.Now); 
    cmd3.Parameters.AddWithValue("@tO", DateTime.Now); 

    int o = cmd3.ExecuteNonQuery(); 
    MessageBox.Show("Good Bye!"); 
    this.Close(); 
    FormCheck f2 = new FormCheck(); 
    f2.Show(); 
} 
catch 
{ 
    MessageBox.Show("Error!"); 
    textBox_VIex.Clear(); 
} 
+1

那麼,問題究竟是什麼? – OldProgrammer

+0

我可以看到一個問題cmd3.Parameters.AddWithValue(「@ id」,1),爲什麼你總是傳遞1? – Devesh

+0

當你說某事不行時,你需要指定發生的事情。你有錯誤嗎?如果你得到一個異常,請添加堆棧跟蹤,所以我們可以幫助你:) – Dzyann

回答

1

請參考我修改了代碼,

 int o = cmd3.ExecuteNonQuery(); 

返回受查詢的行數的計數。如果它爲零,則意味着該ID不在數據庫中。

try 
    { 
    SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True"); 
    con3.Open(); 
    SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE [email protected]",con3); 
    cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text); 

    SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET [email protected],[email protected] WHERE [email protected]", con3); 
    cmd3.Parameters.AddWithValue("@id", int.Parse(textBox_VIex.Text)); 
    cmd3.Parameters.AddWithValue("@dO", DateTime.Now); 
    cmd3.Parameters.AddWithValue("@tO", DateTime.Now); 

    int o = cmd3.ExecuteNonQuery(); 
    if(o > 0) 
     MessageBox.Show("Good Bye!"); 
    else 
     MessageBox.Show("Error!"); 
     this.Close(); 
     FormCheck f2 = new FormCheck(); 
     f2.Show(); 
    } 
    catch 
    { 
    MessageBox.Show("Error!"); 
    textBox_VIex.Clear(); 
    } 
+0

謝謝Devesh –

0

查看代碼中的評論。 首先,這是處理連接的好方法,等等。

int? result = null; // note nullable int 
try 
{ 
    Using (SqlConnection con = New SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True")) 
    { 
     con.Open(); 
     // You don't really care to check up front if the record with id=xx exists because 
     // if it doesn't, you get 0 records updated 
     // unless you do something with the result of that query 
     Using (SqlCommandcmd As New SqlCommand("UPDATE Visitors SET [email protected],[email protected] WHERE [email protected]", con)) 
     { 
      cmd.CommandType = CommandType.Text; 
      cmd.Parameters.AddWithValue("@id", 1); // <<-- Are you always passing "1"????? 
      cmd.Parameters.AddWithValue("@dO", DateTime.Now); 
      cmd.Parameters.AddWithValue("@tO", DateTime.Now);   

      result = cmd.ExecuteNonQuery(); 
     } 

     con.Close() 
    } 
} 
catch 
{ 
    // optionally, log error or show error msg in second msgBox below. Save it to a variable here 
} 

// Separate query and result processing 

// Check the result. If result is still null, your query failed. If not 0 - you updated your records 
if (result != null && (int)result > 0) 
{ 
    MessageBox.Show("Updated record OK"); 
} 
else 
{ 
    if (result == null) 
    { 
     MessageBox.Show("The construct failed"); // optionally add error msg here 
    } 
    else 
    { 
     MessageBox.Show("The record I was looking for is not in DB"); 
    } 
}  
// I don't know what you do with your form logic 
this.Close();