简体   繁体   English

在C#中使用OleDbDataAdapter的参数

[英]Using Parameters with OleDbDataAdapter in C#

I'm using OleDb to populate a DataTable. 我正在使用OleDb来填充DataTable。 I'm trying to use a parameterized query, but it doesn't seem to work with a OleDbDataAdapter. 我正在尝试使用参数化查询,但它似乎不适用于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

See http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx : 请参阅http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx

"The OLE DB.NET Framework Data Provider uses positional parameters that are marked with a question mark (?) instead of named parameters." “OLE DB.NET框架数据提供程序使用标有问号(?)而不是命名参数的位置参数。”

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. 因此,您无法使用@Parameter语法,您必须使用问号指示参数,并以与查询中显示的完全相同的顺序分配参数值。

For those looking for help when their query doesn't even have a parameter, check if your column names are valid. 对于那些在查询甚至没有参数时寻求帮助的人,请检查列名是否有效。

see @TheTerribleProgrammers answer at OleDbException: no value given for parameters when no parameters are defined 请参阅@TheTerribleProgrammers在OleDbException处回答:没有定义参数时没有给出参数值

public static DataTable getDataGridList(string strCmd)
    {

        openConnection(conn);
        OleDbDataAdapter DADet = new OleDbDataAdapter(strCmd, conn);
        DataTable DTDet = new DataTable();
        DADet.Fill(DTDet);
        closeConnection(conn, null);
        return DTDet;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM