简体   繁体   English

使用OleDbDataAdapter的Fill方法的OleDBException

[英]OleDBException using Fill method of OleDbDataAdapter

I'm using a OleDbDataAdapter object to fetch certain data from a database and place it in a data set object. 我正在使用OleDbDataAdapter对象从数据库中获取某些数据并将其放置在数据集对象中。 The code for this is presented below: 如下所示:

oleConn = new OleDbConnection(WebConfigurationManager.ConnectionStrings["PTDB-ConnectString"].ConnectionString);
oleConn.Open();
oleComm = new OleDbCommand("spGetPartRevisionFromSN @SerialNumber = " + SN, oleConn);
oleComm.CommandType = CommandType.Text;
ds = new DataSet();
da = new OleDbDataAdapter(oleComm);
da.Fill(ds); //<--OleDbException occurs here

My problem is that when the program reaches the Fill method of the data adapter, I receive an OleDbException with the error being "Incorrect syntax near 'SN' ", where SN is an alphanumeric serial number. 我的问题是,当程序到达数据适配器的Fill方法时,我收到一个OleDbException,错误是“ SN附近的语法不正确”,其中SN是字母数字序列号。 I have a few SNs that I'm testing, the pattern I notice is that the method seems to run fine with an SN that begins with a letter, eg "J123456", but this exception gets thrown for every SN that begins with a number, eg "1ABCDEF". 我正在测试一些SN,我注意到的模式是,该方法对于以字母开头的SN(例如“ J123456”)似乎运行良好,但是对于以数字开头的每个SN都会抛出此异常,例如“ 1ABCDEF”。 I'm not alterting the SNs, which are just strings, in any way and I've run the SELECT query, defined in the stored procedure, in SQL Server Management Studio with both kinds of SNs without a problem. 我不会以任何方式更改仅是字符串的SN,并且在SQL Server Management Studio中使用两种SN运行存储过程中定义的SELECT查询都没有问题。 Is there something I'm missing? 有什么我想念的吗?

Try this: oleComm = new OleDbCommand("spGetPartRevisionFromSN @SerialNumber = '" + SN.tostring + "'", oleConn); 试试这个:oleComm = new OleDbCommand(“ spGetPartRevisionFromSN @SerialNumber ='” + SN.tostring +“',oleConn);

String assignments and evaluations should be included in single quotes. 字符串分配和评估应包含在单引号中。 I believe that @SerialNumber is a string. 我相信@SerialNumber是一个字符串。

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

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