-
Answer
I am trying to insert a variable in a SQL string but am not having any success in finding the proper sytax. I am coding in C++. Listed below is code section I am trying to figure out. This code is part of an ADO routine that adds a field in an Access Database.
Code:
char Value[40];printf("Input A String");scanf("%s",Value);sSQLCommand = L"ALTER TABLE TableIn ADD @Value nvarchar(20)";com->CommandText = sSQLCommand;
Unfortunately what gets added to the database as a field is @Value.
After some corrections, the following is the corrected code that executes in sSQLCommand:
char sSQLCommand[100];
char TempField[50];
printf("Input Field");
scanf("%s",TempField);
sprintf_s(sSQLCommand,"ALTER TABLE TableIn ADD %s nvarchar(20)",TempField);
-
Can you paste readable code please? It should not be all on one line.
Edit: Oh can we take your edit to mean that you've solved the problem?
-
What kind of API are you using to connect to your database? "com->CommandText" sounds a lot like .NET. If so, you need to add the value and name of your parameter to the "Parameters" collection of your command. Either way, your programm doesn't make an automatic connection from your C++ variable named "Value" to your SQL-Variable of the same name just by name. You need to connect them somehow, details depend on the API.
-
1. Indent your code so that it could be readable.
2. Look into your code, i believe there are certain errors that make you stop to perform it's functionality to the proper end...