Accessing data from a database
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Accessing data from a database

  1. #1
    Join Date
    Dec 2005
    United Kingdom

    Accessing data from a database


    I'm trying to grab data from specific cells in a database and put them into a variable within an application. I'm creating an application that allows a user to input an id have the name associated with it returned to them, so basic database functions really. Except I'm not sure how to go about it.

    I can pass SQL statements to the database, so basically I can alter the database with delete and insert commands, but I'm not sure what's happening when I pass a command that returns a value can I access that value and assign it to a variable?

    I've tried SQLBindCol() and SQLGetData(), but admittedly I don't have a full understanding how these work and I'm just getting a bunch of numbers, not the values expected.

    Regarding the code below, that's about where I'm at now. Thing is, the SQL statement is specific and should only return a single value, so I'm not sure how that works in conjunction with SQLGetData, which, since you have to specify a column number I assume it must expect a row(s) of data. Or am I off base here?

    Any pointers appreciated.

    UCHAR firstName[128];
    SDWORD cbFirstName;
    string statement = "SELECT first_name FROM directory WHERE id = '123'";
    retcode = SQLAllocStmt (hDBC, &hStmt);
    retcode = SQLPrepare(hStmt,(SQLCHAR*)statement.c_str(),statement.size());
    retcode = SQLExecute (hStmt);
    retcode = SQLFetch (hStmt);
    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    	SQLGetData(hStmt, 1, SQL_C_CHAR,firstName, 128, &cbFirstName);
    	retcode = SQLFetch (hStmt);
          cout<<"firstName: "<<firstName<<endl<<"cbFirstName: "<<cbFirstName<<endl;

  2. #2
    Join Date
    Jun 2005
    Chennai , India

    Thumbs up Re: Accessing data from a database

    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people

  3. #3
    Join Date
    Dec 2005
    United Kingdom

    Re: Accessing data from a database

    Thanks for the input. But how might I go about this using ODBC SQL calls? Without using other class libraries?

    I think I've got it working, kinda. Instead of calling a specific field, like in the code above, and instead call the row, I can then use SQLBindCol() to select the name field using the column number:

    string function = "SELECT * FROM directory WHERE id = 123";
    SQLBindCol (hStmt, 2, SQL_C_CHAR, firstName, sizeof(firstName), &cbFirstName);
    I wouldn't have thought that was the most efficient way of doing things, but that that's the best I've got to go on at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center