I'm using a OleDbDataAdapter object to fetch certain data from a database and place it in a data set object. 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. 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 开发者_JAVA百科with a letter, e.g. "J123456", but this exception gets thrown for every SN that begins with a number, e.g. "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. Is there something I'm missing?
Try this: 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.
精彩评论