Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to connect?

Status
Not open for further replies.

jyotsna24

Programmer
Jun 24, 2001
15
IN
Hi,
How to connect c++ with database?Pls write an example program.
Jyotsna.
 
This is a short sample of using ADO and ATL to connect to a SQLServer database

#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include<atlbase.h>
#include<adoint.h>
using namespace std;


int main(int argc, char* argv[])
{
CoInitialize(0);
{
CComPtr<_Connection> pConn;
CComPtr<IUnknown> pUnk;
HRESULT hr=pConn.CoCreateInstance(L&quot;ADODB.Connection&quot;,0,CLSCTX_INPROC_SERVER);
if(FAILED(hr))
{
cout<<&quot;failed&quot;<<endl;
}
hr=pConn->Open(SysAllocString(L&quot;driver={SQL Server};server={ionf};uid={sa};pwd=mypwd;database=GENTRAN;&quot;),
SysAllocString(L&quot;sa&quot;),SysAllocString(L&quot;cereus&quot;),0);
if(FAILED(hr))
{
cout<<&quot;failed&quot;<<endl;
}
hr=pConn->Execute(SysAllocString(L&quot;create table x int,b char(1))&quot;),0,0,0);
if(FAILED(hr))
{
cout<<&quot;failed&quot;<<endl;
}
}
CoUninitialize();
return 0;
}

struct counter
{
char name[25];
short seats;
};
void tttmain()
{
ifstream inFile(&quot;dd.cpp&quot;);

return;
counter xxx;
if(inFile.fail())return;
while(!inFile.eof())
{
inFile.get(xxx.name, 25, '#');
inFile.ignore(1);
inFile >> xxx.seats;
inFile.ignore(100, '\n');
}
{
cout << endl << &quot;Would you like to search for a name?&quot;;
cin >> xxx.name;
cin.ignore(100, '\n');
}
counter ddd[25];
char found=0;
short answer;
int total=0;
while(toupper(xxx.name[0]) == 'Y')
{
char newname[50];
cout << &quot;Please enter a search name: &quot;;
cin.getline(newname, 25);
for(short x = 0; x < 25; x++)
{
int numseats=0;
int total =0;
if(!strcmp(xxx.name,ddd[x].name))
{
numseats = ddd[x].seats;
total += numseats;
found = 'Y';
break;
}else
{
found = 'N';
}if(found == 'Y')
{
cout << &quot;The name &quot; << newname << &quot; exists.&quot; << endl;
cout << &quot;Would you like to enter another name?&quot;;
cin >> answer;
cin.ignore(100, '\n');
}
else
{
cout << &quot;That name does not exist in the database.&quot; << endl;
cout << &quot;Would you like to enter another name?&quot;;
cin >> xxx.name;
cin.ignore(100, '\n');
}
}
cout << &quot;The total number of seats is: &quot; << total << endl;
inFile.close();
}
if(0);
else
cout << &quot;Error opening data file!&quot; << endl;
}
John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top