Variable In SQL String

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.

1
2
3
4
5
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.
I am coding in C++.


Then you should use std::string, std::cout, and std::cin instead of the C stuff you are using.

Anyway, as to your problem...the "L" makes the string "wide", which means you cannot use char* with it anyway. I would suggest putting it all into an std::string (or wstring if you have to), and then setting the text using the .c_str() method.
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);
If you really want to enjoy C++. instead of using C. The job could be done as following:

string sSQLCommand, TempField;
cout << "Input Field ";
cin >> TempField;
sSQLCommand = "ALTER TABLE TableIn ADD " + TempField + "nvarchar(20)"

Later on, you just need sSQLCommand.c_str() if you want to use equivalent C string.

One of many advantages to use string is: you don't need to worry about the possible over writing for your volunerable arraies.


b2ee
Last edited on
Topic archived. No new replies allowed.