Variable In SQL String

This is a discussion on Variable In SQL String within the C++ Programming forums, part of the General Programming Boards category; I am trying to insert a variable in a SQL string but am not having any success in finding the ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    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);
    Last edited by mcertini; 03-24-2011 at 12:10 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    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.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21