Variable In SQL String
-
I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.
hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
-
I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.
hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
You need to enclose your varchar value in single quotes.
hr = rec2->Open("INSERT INTO mytable(id)VALUES('"+ text +"')",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
I find string.Format() to be VERY useful!
Never underestimate the power of human stupidity RAH
-
I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.
hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
in SQL string literals need quotes, so I would suggest you try:
...VALUES('"+ text +"')"...
^ ^Warning: make sure not to insert anything between the single and double quotes there! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
in SQL string literals need quotes, so I would suggest you try:
...VALUES('"+ text +"')"...
^ ^Warning: make sure not to insert anything between the single and double quotes there! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc, Thank you for your reply.
-
Luc, Thank you for your reply.
You're welcome. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
in SQL string literals need quotes, so I would suggest you try:
...VALUES('"+ text +"')"...
^ ^Warning: make sure not to insert anything between the single and double quotes there! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc, I am getting the error message: "error C2110: '+' : cannot add two pointers"
-
Luc, I am getting the error message: "error C2110: '+' : cannot add two pointers"
one cannot add pointers together, it doesn't make sense; adding house numbers wouldn't make sense either. What is your code, and what is the exact line where the error occurs? and what language and IDE are you using? :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
one cannot add pointers together, it doesn't make sense; adding house numbers wouldn't make sense either. What is your code, and what is the exact line where the error occurs? and what language and IDE are you using? :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc, The error is occuring at VALUES('"+ text + "'). My VS2008 is giving me this error. I am trying to send a record to MS Access.
-
I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.
hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);
You cannot directly use local variables inside your SQL statements. You can use a Parameter object to pass a value from a variable to the statement. For example see, http://msdn.microsoft.com/en-us/library/ms677589(VS.85).aspx[^]
The need to optimize rises from a bad design.My articles[^]