473,385 Members | 2,005 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,385 software developers and data experts.

Accessing Database from c++

Hai All,

Can anyone suggest or provide me sample code for making ODBC
connection and executing simple queries from c++. I am using sql server
2000. Also please tell what are the header files to be included. I
created a DSN and now I need to make connection with that DSN and
execute query from my code.

Thanks in advance..
Please help!!!! Looking forward for the response.

Jun 16 '06 #1
4 4853
I can provide you a little example ... not in C++ but in C .... you can
modify it and reuse in C++ code.... the only problem is that this
example make an oracle connection... but it should be quite the same...
only the string should change
bye
Pier Paolo

Wallace ha scritto:
Hai All,

Can anyone suggest or provide me sample code for making ODBC
connection and executing simple queries from c++. I am using sql server
2000. Also please tell what are the header files to be included. I
created a DSN and now I need to make connection with that DSN and
execute query from my code.

Thanks in advance..
Please help!!!! Looking forward for the response.

#include <stdio.h>
#include "sqlmine.h"
struct data {
char nome[1024];
int nomelen;
SQL_TIMESTAMP_STRUCT ora;
int oralen;
} data1;

void usa(void *altro)
{
printf("nome %s data creazione ",data1.nome);
if (data1.oralen!=0)
printf("%d-%d-%d:%d:%d:%d\n",data1.ora.year,data1.ora.month,data 1.ora.day,data1.ora.hour,data1.ora.minute,data1.or a.second);
}

SQLRETURN sqltrybind(SQLHANDLE *sqlstmt)
{
int res;
res= SQLBindCol(sqlstmt,
1,
SQL_C_CHAR,
&(data1.nome),
1024,
&data1.nomelen);

res=res+SQLBindCol(sqlstmt,
2,
SQL_C_TYPE_TIMESTAMP,
&(data1.ora),
1024,
&data1.oralen);
return res;
}

int sqlconnect(char *server,char *user,char *pwd)
{
int res;
if (res=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE ,&sqlenv)!=SQL_SUCCESS) printf("Errore in allocEnv\n");
if (res=res*SQLSetEnvAttr( sqlenv,SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER)!=SQL_SUCCESS) printf("Errore in SQLATTREnv\n");
if (res=res*SQLAllocHandle(SQL_HANDLE_DBC,sqlenv,&sql conn) != SQL_SUCCESS)
{
printf("Errore in allocConn %d\n",res);
}
if (res=res*SQLConnect(sqlconn,server,strlen(server), user,strlen(user),pwd,strlen(pwd))!=SQL_SUCCESS) printf("Errore in Connessione\n");
return res;
}

/* Esegue Select */
void sqlselect(char *select, sqlbind sqlb,void *altro,usedata used)
{
int res;
SQLHANDLE sqlstmt;
res=SQLAllocHandle(SQL_HANDLE_STMT,sqlconn,&sqlstm t);
if (res==SQL_SUCCESS) printf("Errore in creazione Handle Statement\n");
res=SQLPrepare(sqlstmt,select,strlen(select));
if (res==SQL_SUCCESS) printf("Errore in creazione SQLPrepare\n");
sqlb(sqlstmt);
SQLExecute(sqlstmt);
while (SQLFetch(sqlstmt)==SQL_SUCCESS)
{
used(altro);
}
SQLFreeHandle(SQL_HANDLE_STMT,sqlstmt);
}

void esegui()
{
sqlconnect("TEST","SYSTEM","MANAGER");
sqlselect("select object_name,created from user_objects",sqltrybind,NULL,usa);
sqlDisconnect();
}

/* Disconnect */
void sqlDisconnect()
{
SQLDisconnect(sqlconn);
SQLFreeHandle(SQL_HANDLE_DBC,sqlconn);
SQLFreeHandle(SQL_HANDLE_DBC,sqlenv);
}
#include <sql.h>
#include <sqlext.h>

typedef SQLRETURN (*sqlbind)(SQLHANDLE *sqlstmt);
typedef void (*usedata)(void *altro);
SQLHANDLE sqlenv;
SQLHANDLE sqlconn;

int sqlconnect(char *server,char *user,char *pwd);
void sqlDisconnect();
void sqlselect(char *select, sqlbind sqlb,void *altro,usedata used);

#include <windows.h>
#include <string.h>
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <commctrl.h>
HINSTANCE hinstp;
void startDebug();
int leggi(HWND hwnd,int obj,char *buffer,int size);
char username[256];
char datasource[256];
char pwd[256];

#include "common.h"

void startDebug()
{
int hCrt,hCrti,hCrte;
FILE *hf,*hfi,*hfe;
AllocConsole();
hCrt = _open_osfhandle((long)GetStdHandle ( STD_OUTPUT_HANDLE),_O_TEXT );
hf = fdopen( hCrt, "w" );
*stdout = *hf;
setvbuf( stdout, NULL, _IONBF, 0 );

hCrti = _open_osfhandle((long) GetStdHandle ( STD_INPUT_HANDLE),_O_TEXT );
hfi = fdopen( hCrti, "r" );
*stdin = *hfi;
setvbuf( stdin, NULL, _IONBF, 0 );

hCrte = _open_osfhandle((long)GetStdHandle ( STD_OUTPUT_HANDLE),_O_TEXT );
hfe = fdopen( hCrte, "w" );
*stderr = *hfe;
setvbuf( stderr, NULL, _IONBF, 0 );

}
int leggi(HWND hwnd,int obj,char *buffer,int size)
{
memset(buffer,0,size);
if (GetDlgItemText(hwnd, obj, buffer, size)) {
printf("%s\n",buffer);
return 1;
}
return 0;
}
Jun 16 '06 #2

Wallace wrote:
Can anyone suggest or provide me sample code for making ODBC
connection and executing simple queries from c++. I am using sql server
2000. Also please tell what are the header files to be included. I
created a DSN and now I need to make connection with that DSN and
execute query from my code.


If you're using MSVC and you are willing to use ADO then it's really
simple. Just use:

#import <c:\program files\common files\system\ado\msado15.dll> rename(
"EOF", "adoEOF" )

Something like this will open a connection:

ADODB::_ConnectionPtr writeCnx;
writeCnx = ADODB::_ConnectionPtr();
writeCnx.CreateInstance( __uuidof( ADODB::Connection ) );
writeCnx->CommandTimeout = 60;
writeCnx->Open( writeDsn().c_str(), L"", L"",
ADODB::adConnectUnspecified );

To execute SQL then use something like this:

ADODB::_RecordsetPtr rs;
rs = writeCnx->Execute( cmd.c_str(), &res, ADODB::adOptionUnspecified
);

I'll leave it as an excercise to get the data out. Read the ADO COM
spec to work it out. It's actually all pretty easy.
K

Jun 16 '06 #3
[ODBC Request and sample code reply redacted]

1. Please do not post attachments to comp.lang.c++. See the FAQ
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4

2. This is Off Topic. See the FAQ
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

A newsgroup with "microsoft", "windows", or "database" in the name would
be more appropriate.
Jun 16 '06 #4

"Wallace" <pr****************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hai All,

Can anyone suggest or provide me sample code for making ODBC
connection and executing simple queries from c++. I am using sql server
2000. Also please tell what are the header files to be included. I
created a DSN and now I need to make connection with that DSN and
execute query from my code.

Thanks in advance..
Please help!!!! Looking forward for the response.


If you are using Windows, I suggest you look at:
http://msdn.microsoft.com/data/learning/MDAC/
Jun 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott Castillo | last post by:
Is it possible to access an SQL Server database from a Javascript client? I know you can transfer variables from middleware to Javascript client, but was wondering if you can directly access...
2
by: Douglas Harber | last post by:
If I have DB2 8.1 (FP5, I believe, but not relevant to my question...I hope) installed on my desktop, do I have what I need to connect to a remote DB2 server (also running 8.1 FP5) from my desktop...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
2
by: DanB | last post by:
I am using VB DotNet Std Edition and am writing a web application that needs to read and write to an password protected database. The mdb files reside in a folder (fpdb) under the localhost...
3
by: mark.jerrom | last post by:
I'm fairly new to this Web Service game so please feel free to suggest something different if it looks like i'm completely off track! I'm trying to write an application that runs on a Pocket PC...
3
by: Pakna | last post by:
Hello, I have what may be a beginner's question regarding DB2. How does one access a remote table on a remote database via SQL? What is the command string, is there an equivalent of Oracle...
5
by: samadams_2006 | last post by:
I'm having a problem in accessing a Microsoft Access Database in a VB.NET Web Application. It's so straight forward, I thought I'd walk you through all the details here: 1) I have a .NET Web...
4
by: scout3014 | last post by:
Hi I am using WAMP5 to create web applications. I have a problem as far as accessing the database is concerned. This following code is used to access database: $con =...
4
by: Noy B | last post by:
Hi, I have developed a small application that is using a MSAccess DB. the problem is that it was developed on a machine where the application and the DB are both located. now it needs to be...
2
by: brendan_gallagher_2001 | last post by:
HI I am developog an ASP.net site (using vb.net 1.1) which will be accessed by a number of different office locations, where certain users will be assigned a authoriser profile, and others will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.