Syntax error in query. Incomplete query clause.
-
07 Maret 2012 3:59
Hello.
I am using an Access .MDB file. I need to get the Name of an entry based on its ID.
My SQL code is as follows:
OleDbCommand cmdGetName = new OleDbCommand("SELECT Name FROM @pTable WHERE GroupKey = @pContactID", dbConnect); cmdGetName.Parameters.Add("@pTable", OleDbType.VarChar); cmdGetName.Parameters.Add("@pContactID", OleDbType.Integer); cmdGetName.Parameters[0].Value = "Group"; //string cmdGetName.Parameters[1].Value = ID; //int value String Name = (String)cmdGetName.ExecuteScalar();The problem is the ExecueScalar() returns an exception error
"Syntax error in query. Incomplete query clause."
It seems that using an integer and a string parameter has a difference.
How can I correct the SQL command?
Thanks.
Semua Balasan
-
07 Maret 2012 4:03
Hello,
You can't use a parameter for the table name. Replace the parameter @pTable with the real table name and try it again; then it will work.
If you want to query different table, you have to build the SQL statement dynamically.
Olaf Helper
* cogito ergo sum * errare humanum est * quote erat demonstrandum *
Wenn ich denke, ist das ein Fehler und das beweise ich täglich
Blog Xing- Diedit oleh Olaf HelperMicrosoft Community Contributor 07 Maret 2012 4:03
- Disarankan sebagai Jawaban oleh Naomi NMicrosoft Community Contributor 07 Maret 2012 4:27
-
07 Maret 2012 4:27
Thanks for the response.
I'm using another SQL command which is similar to this, except that instead of using an integer "ID" for the search criteria, all the parameters are strings.
The only difference here is that the ID is an integer, not a string.
-
07 Maret 2012 4:31
Try:
OleDbCommand cmdGetName = new OleDbCommand("SELECT Name FROM Group WHERE GroupKey = @pContactID", dbConnect); cmdGetName.Parameters.Add("@pContactID", OleDbType.Integer); cmdGetName.Parameters[0].Value = ID; //int value String Name = (String)cmdGetName.ExecuteScalar();
For every expert, there is an equal and opposite expert. - Becker's Law
My blog- Ditandai sebagai Jawaban oleh KJian_ 13 Maret 2012 3:27