OleDbException: Data type mismatch in criteria expression

不打扰是莪最后的温柔 提交于 2019-12-25 04:38:07

问题


I read data from MS Access using C#. But get the OleDbException trying to execute such query:

SELECT * FROM Flats 
WHERE Flats.VersionStamp <= [theDate] AND Flats.Flat=[theFlat]

OleDbException:

Data type mismatch in criteria expression.

On the other side, any one of the following queries works fine:

SELECT * FROM Flats 
WHERE Flats.VersionStamp <= [theDate] AND Flats.Flat=1

SELECT * FROM Flats 
WHERE Flats.VersionStamp <= #1/1/2009# AND Flats.Flat=[theFlat]

The C# code stays the same all the time:

DbParameter theFlat = new OleDbParameter("theFlat", 1);
DbParameter theDate = new OleDbParameter("theDate", new DateTime(2009, 1, 1));

using (DbDataReader reader = dbHelper.ExecuteReader(sqlText, theFlat, theDate))
{ }

Finally, the query can be successfully executed directly in the MS Access UI.

What is wrong here?


回答1:


I am not sure but I don't think the OleDb classes support named parameters. Try the following SQL instead:

SELECT * FROM Flats WHERE Flats.VersionStamp <= ? AND Flats.Flat=?

The parameters must be added to the command object in the right order (I don't see you adding the parameters in your code).




回答2:


Where are you defining/using the parameters in your SQL String; I don't see them.

Try this:

SELECT * From Flats WHERE VersionStamp = @theDate AND Flat = @theFlat

DbParameter = new OleDbParameter ("@theDate", someDate);



回答3:


http://support.microsoft.com/default.aspx?scid=kb;en-us;316744

Contrary to what the preceding documentation error describes, the OleDbCommand parameters are positional when they are used with the Microsoft SQL Server OLE DB provider. The names of the parameters can be arbitrary... The order of the parameters that you add to the OleDbParameterCollection must match the order of the parameters in your stored procedure.



来源:https://stackoverflow.com/questions/650282/oledbexception-data-type-mismatch-in-criteria-expression

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