473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
0 1622

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

Similar topics

2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
5
by: Todd Huish | last post by:
I have noticed something disturbing when retrieving datasets over a relatively slow line (multiple T1). I am looking at about 25 seconds to retrieve 500 rows via a php-odbc link. This same select...
3
by: Reza | last post by:
Hello I tried this friday, but didn't get anywhere so trying again Basically, I have a fixed list of people that can access the application in the Intranet, and with the policy of the company the...
9
by: gdonald | last post by:
Hi Wonder if anyone can help me. I am running an access 2000 application with the backend on a server running windows 2003 and a seperate front end on each users machine. When i designed it,...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
4
by: Gregory Gadow | last post by:
If there is a more appropriate forum, please let me know and I will post there. Our field reps can go on to our website and select from several sets of data to create the address we then provide...
4
by: rn5a | last post by:
Assume that a ASPX page uses a user control named Address.ascx which has 2 TextBoxes. This ASCX page creates 2 properties named 'Address' & 'City' using the Get & Set statements: <script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.