string date=
DateTime.Now.ToString("d.M.yyyy",System.Globalization.DateTimeFormatInfo.InvariantInfo);
String MyString = @"UPDATE cas SET Odhod= '" + label1.Text + "'
WHERE sifra = " + textBox1.Text + " and Datum = "+date+"";
当我执行此更新而没有Datum
(德语为“日期”)时,它可以正常工作,但是Datum
它不起作用。我已连接到Accesss数据库,表字段Datum
的类型为Date/Time
。
以下是该计划: https://www.dropbox.com/s/hx4zduvul8mh2uy/8.4.zip
Pictre of problem: http://img43.imageshack.us/img43/5189/errorbh.jpg
答案 0 :(得分:0)
缺少一些撇号?
String MyString = @"UPDATE cas SET Odhod= '" + label1.Text + "'
WHERE sifra = " + textBox1.Text + " and Datum = '"+date+"'";
答案 1 :(得分:0)
使用参数化查询。您不必担心日期格式,它将使您的查询SQL注入安全。
string fileName = @"C:\mydb.accdb";
string query = "UPDATE cas SET Odhod = ? WHERE sifra = ? AND Datum = ?";
using (OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName)) {
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("?", label1.Text);
cmd.Parameters.AddWithValue("?", textBox1.Text);
cmd.Parameters.AddWithValue("?", date);
conn.Open();
cmd.ExecuteNonQuery();
}