By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

Windows Dynamic Link Library - Connect to SQL Server and Retrieve records

P: 30
Hi,

I want to create a Windows DLL using VC++ 6.0 which connects to SQL Server.
After establishing the connection, I want to retrieve records from table (query) and out put the recordset to a text file.

Can you any expert, give me ideas as to how to go about?

I have used the following code for connection and connection is successful. I m able to insert records (i got the code from codeproject website). I was successfully able to insert records, but i want to retrieve records and output to text file.
How do i do?
Can anyone, please help?

Padhu

--------Code-------
#include <windows.h>

#include <sqlext.h>

#include <stdio.h>
#include <string.h>


int main(void)

{

HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL;// Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "Test";// Data Source Name buffer
UCHAR szUID[10] = "sa";// User ID buffer
UCHAR szPasswd[10] = "padhutest";// Password buffer
UCHAR szModel[128];// Model buffer
SDWORD cbModel;// Model buffer bytes recieved
char buff[9] = "Testing";


UCHAR szSqlStr[128]= "INSERT into myTest (userid,date_time) Values (2,2006/01/01)" ;
//UCHAR szSqlStr[128]= "SELECT * from myTest " ;


RETCODE retcode;

//sprintf((char*)szSqlStr,"INSERT into <Tablename> (Colname) Values ('%s')",buff);


// Allocate memory for ODBC Environment handle

SQLAllocEnv (&hEnv);


// Allocate memory for the connection handle

SQLAllocConnect (hEnv, &hDBC);



// Connect to the data source "test" using userid and password.

retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);



if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)

{

// Allocate memory for the statement handle

retcode = SQLAllocStmt (hDBC, &hStmt);



// Prepare the SQL statement by assigning it to the statement handle

retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));



// Execute the SQL statement handle

retcode = SQLExecute (hStmt);



// Project only column 1 which is the models

SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);



// Get row of data from the result set defined above in the statement

retcode = SQLFetch (hStmt);


// Free the allocated statement handle

SQLFreeStmt (hStmt, SQL_DROP);


// Disconnect from datasource

SQLDisconnect (hDBC);

}



// Free the allocated connection handle

SQLFreeConnect (hDBC);



// Free the allocated ODBC environment handle

SQLFreeEnv (hEnv);

return 0;

}
Jan 4 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.