1

I get date from DateEdit and try to Store into Access Database. But it show error like this

Syntax error in INSERT INTO statement.

my insert statement is this

 OleDbCommand top = new OleDbCommand("INSERT INTO invoice(invoice_number,order_number,customername,status,subtotal,tax,total,date) VALUES (" + inno + "," + odrno + ",'" + name + "','"+ chk1 +"' ,"+ subtottal +","+ tax +","+total+",'"+date+"')", conn);
 top.ExecuteNonQuery();

Except Date remaining values store successfully but how can i store date ??

I get date like this DateTime date = dateEdit1.DateTime;

Help me.

4

1 回答 1

5

DATEreserved keyword用于 Microsoft Access 的。您应该将它与方括号一起使用,例如[DATE]

而且您应该始终使用parameterized queries. 这种字符串连接对SQL Injection攻击开放。

OleDbCommand top = new OleDbCommand(@"INSERT INTO invoice(invoice_number,order_number,customername,status,subtotal,tax,total,[date]) 
                                     VALUES (@invoice_number, @order_number, @customername, @status, @subtotal, @tax, @total, @date)", conn);
top.Parameters.AddWithValue("@invoice_number", inno);
top.Parameters.AddWithValue("@order_number", odrno);
top.Parameters.AddWithValue("@customername", name);
top.Parameters.AddWithValue("@status", chk1);
top.Parameters.AddWithValue("@subtotal", subtotal);
top.Parameters.AddWithValue("@tax", text);
top.Parameters.AddWithValue("@total", total);
top.Parameters.AddWithValue("@date", date);

作为一般建议,不要对数据库中的标识符和对象名称使用保留关键字。

于 2013-11-05T12:31:16.147 回答