2014-09-10 114 views
-4
string query2 = "INSERT INTO library_database.status_of_issue VALUES('"; 
query2 = query2 +textBox2.Text + "','"; 
query2 = query2 + textBox1.Text + "', CURDATE(),ADDDATE(CURDATE(), INTERVAL 14 DAY)"; 
cmd = new MySqlCommand(query2, con); 
MySqlDataReader d1 = cmd.ExecuteReader(); 
MessageBox.Show("Issed..."); 
d1.Close(); 
+0

錯誤執行來,有在SQL語法錯誤。 – 2014-09-10 16:03:21

+0

你可以添加錯誤信息嗎? – EWit 2014-09-10 16:07:18

回答

0

缺少的價值觀條款收盤parenthesys,但您的查詢應該被重寫,以避免Sql Injection和INSERT查詢與ExecuteNonQuery

string query2 = @"INSERT INTO library_database.status_of_issue VALUES(@p1, @p2, 
        CURDATE(),ADDDATE(CURDATE(), INTERVAL 14 DAY))"; 
cmd = new MySqlCommand(query2, con); 
cmd.Parameters.AddWithValue("@p1", textBox2.Text); 
cmd.Parameters.AddWithValue("@p2", textBox1.Text); 
int rows = cmd.ExecuteNonQuery(); 
if(rows > 0) 
    MessageBox.Show("insert OK..."); 
0

所以非常明顯。您錯過了VALUES的結尾部分。這應該工作:

string query2 = string.Format("INSERT INTO library_database.status_of_issue VALUES('{0}', '{1}', CURDATE(), ADDDATE(CURDATE(), INTERVAL 14 DAY))", textBox2.Text, textBox1.Text); 

using(var cmd = new MySqlCommand(query2, con)) 
{ 
    if(cmd.ExecuteNonQuery() > 0) 
     MessageBox.Show("Issed..."); 
} 

另請注意,INSERT,UPDATE和DELETE命令應使用ExecuteNonQuery()執行。

相關問題