在C#中将参数与OleDbDataAdapter一起使用 [英] Using Parameters with OleDbDataAdapter in C#
问题描述
我正在使用OleDb填充数据表.我正在尝试使用参数化查询,但它似乎不适用于OleDbDataAdapter.有人有建议吗?
I'm using OleDb to populate a DataTable. I'm trying to use a parameterized query, but it doesn't seem to work with a OleDbDataAdapter. Anyone have any suggestions?
cmd.CommandText = "SELECT A,B,C,D FROM someTable WHERE A=@A AND D BETWEEN @D1 AND @D2";
cmd.Parameters.Add("@A", OleDbType.VarChar).Value = "1234567";
cmd.Parameters.Add("@D1", OleDbType.DBDate).Value = "02/01/2011";
cmd.Parameters.Add("@D2", OleDbType.DBDate).Value = "01/31/2012";
A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
System.Data.OleDb.OleDbException (0x80040E11): [DB2] SQL0206N "@A" is not valid in the context where it is used. SQLSTATE=42703
推荐答案
请参见 http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx :
"OLE DB.NET Framework数据提供程序使用带有问号(?)而不是命名参数的位置参数."
"The OLE DB.NET Framework Data Provider uses positional parameters that are marked with a question mark (?) instead of named parameters."
因此,您不能使用@Parameter
语法,必须用问号指示参数,并按照与查询中出现的顺序完全相同的顺序分配参数值.
So you cannot use the @Parameter
syntax, you have to indicate parameters with question marks, and assign your parameter values in the exact same sequence as they appear in the query.
这篇关于在C#中将参数与OleDbDataAdapter一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!