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

Accessa SQL database using Visual C++

Status
Not open for further replies.

howard7890

Programmer
Jun 6, 2003
3
0
0
US
I am new to Visual C++ and do not know the syntax to read records from my SQL database.

I have a database called "SLL"
name = howard
password = howard1
record = tests
record section = names

Could anyone give me a sample using these parameters.
I have acomplished this in visual basic but C++ looks complex.

Visual basic = (
Set con = New ADODB.Connection
con.Open "sql2000", "howard", "howard1
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
SQLstring = "Select * from oslist where osname = 'Windows 2000 Server'"
rst.Open SQLstring, con, adOpenStatic, adLockReadOnly)

How can I accomplish the same thing in VC++?
What include files do I need?


 
to use ado the same way in VC put this in your stdafx.h file at the end

#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
no_namespace \
rename("EOF", "adoEOF")


change the path to find where your actual file is, of course

make a connection object like this:
_ConnectionPtr g_Cnn;
g_Cnn.CreateInstance(__uuidof(Connection));
CString strconn = _T("Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=username;Initial Catalog=BugReporter;Data Source=YourServerName");

HRESULT hr = g_Cnn->Open(strconn.AllocSysString(),_T(""),_T(""),0);

Use recordset like this:

_RecordsetPtr rst;
VARIANT *var = NULL;

strSQL = _T("select * from mytable");
rst.CreateInstance(__uuidof (Recordset));
rst->CursorLocation = adUseClient;
rst = g_Cnn->Execute(strSQL.AllocSysString(),var,0L);

FieldsPtr pFields;
FieldPtr pID;

pFields = rst->GetFields();
pID = pFields->GetItem(_T("PersonID"));
nVersionID = (int)pID->Value;

you should really use _variant_t variable first then check if v.vt == VT_NULL; before actually getting the value, because if it's null, you'll get an error trying to put it into an int. Hope this helped

bdiamond
 
One note - you say SQL; however, you didn't say Microsoft SQL Server. The connection string can vary depending on the source of your SQL data.

For instance, I build my ADO database definitions with M/S Access 97 and use the M/S Jet engine to test my C++ code. The Jet engine even works fine in production for most of my customers; however, the M/S Jet engine has a 100 field per table limit and also doesn't allow multiple user access.

One of my applications exceeded this Jet engine field limit and we transferred the database to the customer's M/S SQL Server with a SQL Server utility. When I did this transfer, I had to change the connection parameters accordingly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top