gpt4 book ai didi

c# - 查询语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:58 25 4
gpt4 key购买 nike

我是编程新手,正在 Access 中开发新的桌面数据库应用程序,我正在尝试将数据插入表中。我有两个日期时间选择器,我从中读取值作为

jobcodedatabean.PaperRecievedate1 = dtpjobcodedate.Value.Date;
jobcodedatabean.Shipmenentdate = dtpshipmentdate.Value.Date;

并且我已经将数据bean传递给了一个函数

 public void addaction(JobCodeDataBean jobcodedatabean)
{
MessageBox.Show(jobcodedatabean.Shipmenentdate.ToString());

try
{

OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
oleDbConnection1.Open();
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO jobcodemastertable (jobcode ,customercode,totaltrip,shipmentdate,fromPlace, destination,description ,packagetype ,noofpackage ,contactperson ,jobecodedate ) Values ('" + jobcodedatabean.Jobcode + "', '" + jobcodedatabean.Customercode + "' ," + jobcodedatabean.Totaltrip + "," + jobcodedatabean.Shipmenentdate + " ,'" + jobcodedatabean.Fromplace + "','" + jobcodedatabean.Destination + "','" + jobcodedatabean.Description + "','" + jobcodedatabean.Typeofpackage + "','" + jobcodedatabean.Noofpackages + "','" + jobcodedatabean.Contactperson + "'," + jobcodedatabean.PaperRecievedate1 + ") ", oleDbConnection1);
oleDbCommand1.CommandType = CommandType.Text;
oleDbCommand1.ExecuteNonQuery();

oleDbConnection1.Close();

}
catch (Exception)
{
MessageBox.Show(e);

}

但我在查询时遇到异常

Syntax error (missing operator) in query expression '2/16/2012 12:00:00 AM'. 

在访问中,日期字段是短日期格式

请有人帮我解决错误

最佳答案

引用不正确。为避免此类错误,请使用有序参数:

var myCommand = new OleDbCommand(
"INSERT INTO MyTable(someDateField, someTextField, someNumberField) VALUES (?, ?, ?)"
);

myCommand.Parameters.Add(DateTime.Now);
myCommand.Parameters.Add("Some text");
myCommand.Parameters.Add(123);

使用参数还有助于防止 SQL injection attacks .在您的示例中,如果其中一个字符串包含撇号,除非您正确地将其转换为两个撇号,否则它将失败。使用参数,这些参数会自动正确转义。

关于c# - 查询语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9319809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com