Error when inserting a record into MS Access

主宰稳场 提交于 2019-12-25 06:44:55

问题


I have val MyDate in my C# program that contain today-date or null.

I have date field in my access 2007 - TdateOpen

I try to insert to the database like this:

SQL = "insert into MyCount(TdateOpen) values ('" + MyDate +"')";

and I get this error:

Data type mismatch in criteria expression

what can be the problem?


回答1:


Coz in your SQL statement you are entering date as String . Instead of String it should be a date/date format. Try to surround by # .




回答2:


You will need to ensure that the date is in US order (mm/dd/yyyy) or ANSI/ISO order, whether you use dash or slash is not important, ANSI/ISO is to be preferred.

Then as, Madhu CM said, the delimiter for dates in Access is hash (#), however, your date can be null and null cannot be delimited, so you will either have to add the delimiter to a date string, if a date is returned, or use two sql statements, one for null and one for date.




回答3:


You could SQL parameters instead of dynamically embedding the date value into the statement.

SQL = "insert into MyCount(TdateOpen) values (?)";
var parameter = yourCommand.CreateParameter();
parameter.Value = yourDateTime;
yourCommand.Parameters.Add(parameter);

(DISCLAIMER: The code was not compiled nor tested, but it should give you a hint)



来源:https://stackoverflow.com/questions/3724015/error-when-inserting-a-record-into-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!