UPDATE查询不使用日期字段

时间:2013-04-09 17:48:59

标签: c# ms-access oledb

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

2 个答案:

答案 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();
}