【发布时间】:2014-11-09 17:25:37
【问题描述】:
我是 Access 和保存数据的新手。我有一个日期时间字段,我在 Visual Studio 中使用它来制作表单并选择日期时间选择器。我不确定我哪里出错了,但我知道这是导致问题的日历选择器。我在插入到语句中得到错误语法错误。
这是我的代码
string When = qaWhendateTimePicker.Value.ToString("HH:mm:ss.fff");
然后是我的保存查询
SQLString = "INSERT INTO QAAnswers (QuestionID,CallMonitorNumber,When) VALUES('"+QuestionID + "','" + CallMonitorNumber + "','" + When + "');";
这当然是缩短版。但是为了我的爱,如果我把它保存到数据库的时间取出来就好了
我拥有的 Access 数据库 WHEN SET AS DATE / TIME。
我应该将其设置为文本吗?我认为不会,因为这样我就无法根据日期提取查询。
提前感谢,因为我已经在这里工作了好几个小时。
更新
根据下面的评论,我更改了语法。这就是我所拥有的。如果我输入 .value 或 .date 它不起作用。我确定这是我做错的事情。我收到错误,无法将参数值从 datetimepicker 转换为 datetime。谢谢,因为我在访问中学到了很多东西。
ad.InsertCommand = new OleDbCommand("insert into QAAnswers ([CallMonitorNumber],[When],[ProperGreeting],[AssureHelp],[AccountVerification],[ConfirmCaller],[ProperPoliciesSolutions],[ProperPoliciesAdmin],[AppropriateTools],[TroubleshootingSteps],[ConfirmResolved],[CustomerEducation],[CSATSurvey],[ThanksCallerBrand],[ProfessionalToneAttitude],[CustomerInvolved],[CallPace],[Empathy],[PhoneEtiquette],[DiffuseEscalated],[UnacceptableCallPractice],[Notes],[ScorePotential],[ScoreActual],[FinalScore]) values (@CallMonitorNumber,@When,@ProperGreeting,@AssureHelp,@AccountVerification,@ConfirmCaller,@ProperPoliciesSolutions,@ProperPoliciesAdmin,@AppropriateTools,@TroubleshootingSteps,@ConfirmResolved,@CustomerEducation,@CSATSurvey,@ThanksCallerBrand,@ProfessionalToneAttitude,@CustomerInvolved,@CallPace,@Empathy,@PhoneEtiquette,@DiffuseEscalated,@UnacceptableCallPractice,@Notes,@ScorePotential,@ScoreActual,@FinalScore)", con);
ad.InsertCommand.Parameters.Add("@QuestionID", OleDbType.VarChar).Value = agentIDNumbertextBox.Text.ToString();
ad.InsertCommand.Parameters.Add("@CallMonitorNumber", OleDbType.VarChar).Value = qaCallMonitorNumbertextBox.Text.ToString();
ad.InsertCommand.Parameters.Add("@When", OleDbType.DBDate).Value = qaWhendateTimePicker;
更新答案
我终于明白了。不确定这是否是最好的方法,但我只是忽略了 datetimepicker。我希望我可以使用日期选择器。相反,我只是使用此语句输入添加日期(缩短版本)注意 [when] 和 Date() 。
DateTime now = DateTime.Now;
ad.InsertCommand = new OleDbCommand("insert into QAAnswers ([CallMonitorNumber],[When]) values (@CallMonitorNumber,Date())", con);
我希望这对其他人有所帮助。我真的不喜欢对数据库使用访问权限,但这就是我必须使用的。
我给 marc 打分,因为我从来没有听说过参数化查询
【问题讨论】:
-
错误说明了什么?
-
查看行的最后一个字符 >>
"');";并删除"; -
您为什么将您的专栏称为
When? -
我可以随便称呼它,但我正在测试。另外,如果我选择“;它会显示错误
-
只需将
,'" + When + "')更改为,#" + When + "#)即可。
标签: c# sql database winforms ms-access-2007