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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ SQL connection

Status
Not open for further replies.

MattNeeley

IS-IT--Management
Mar 7, 2004
94
0
0
US
I tried following the FAQ but it's a little complex. Was wondering if anyone could break it down a little for me or had a simpler way to connect from VC++ 6 to a SQL 2000 database on a remote host across the internet. I'm pretty new to VC++ 6 so any help or tuts would be greatly appreciated. I've got most of the basics down of strings and finding things in strings etc. But the specifics of making the actual connection from VC++ code to the remote DB elude me. Thanks


Matt
 
What is written in FAQ there inmy opinion is one of most simpliest explanations you may find in internet, so break your mind, read it and follow up.

Ion Filipski
1c.bmp
 
i took a very similar Faq and broke it down into functions. I dont have the select statement function done yet but the following two are a connection function and a function to support linear queries (update, insert, delete)

void DbConnect(_ConnectionPtr& conn){
string sTempConn;
string query;
HRESULT hr=S_OK;
_variant_t vRecordsAffected;

CoInitialize(NULL);
conn.CreateInstance(_uuidof(Connection));
sTempConn = "Provider='sqloledb';Data Source='";
sTempConn += "SERVERNAME or ADDRESS";
sTempConn += "';Initial Catalog='";
sTempConn += "FileScanner";
sTempConn += "'; Integrated Security='SSPI';";


try
{
hr = conn->Open(sTempConn.c_str(), "USERNAME", "PASSWORD", adConnectUnspecified);
}
catch (...)
{
cout<<"Error Connecting to DB"<<endl;
}
CoUninitialize();
}

int LinearQuery(string query, _ConnectionPtr& conn)
{
_variant_t vRecordsAffected;
try {
conn->Execute(query.c_str(), &vRecordsAffected, 0);
}
catch (...) {
return -1;
}
return vRecordsAffected.intVal;
}
 
hmm i cant edit my previous post apparently so just change one thing. In the connection function the line that reads

sTempConn += "FileScanner";

should read

sTempConn += "DATABASE_NAME";
 
What about #includes?...I know about SQL.h any others I need to access a SQL DB or that would be useful?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top