By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,412 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,412 IT Pros & Developers. It's quick & easy.

Seeking Simple Tutorial for Connecting to SQL via C++ (VS2005)

P: n/a
Hi,

I've done several database apps with PHP to SQL, or C++ to MS Access,
etc. And I went to the VS2005 / SQL 2005 launch event where the
presented built a simple DB app in seconds on stage. (Though he did it
with .asp for a web site rather than C++)

Now, I am trying to use a C++ / MFC framework I have to make an app
that needs to connect to SQL. But... When I fire up Visual Studio and
search around the MSDN library, I can't seem to figure it out.

Ideally, can someone point to a web site (or just post some code). I
would also love to hear a recomendation for an "in 21 days" type book
on this.

Thanks in advance for any help.
Nov 11 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 11, 2:38 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
One option is used managed C++ and .Net. That would permit you to use
SqlClient from ADO .Net, which I think is a very good API.

If you want to work with native code, you two have options ODBC and OLE DB.
I will try ADO.net, but that is probably going to take a bit more
learning... I have never used either .net, or managed C++ before. But
though I forgot to say this in my original post, I am hoping to figure
out how to use ADO (not .net) for now.

I've included "msado25.h", which gives access to the _Connection and
_Recordset objects. And, I've found numerous examples for using it
from VB on the web which I have attempted to translate into C++, such
as:

CString csServer = _T("TestServer");
CString csDatabase = _T("TestDatabase");
CString csSQLConnectString;
csSQLConnectString.Format(_T("Provider=MSDATASHAPE ;Data
Provider=SQLOLEDB;")
_T("server=%s;Database=%s;Integrated Security='SSPI';"),
m_csServer, m_csDatabase);
CString csQuery = _T("SELECT * FROM Employees");

VARIANT vSQLVariant;

_Connection sqlConnection;
_Recordset sqlRecordset;

// In VB it would be:
// sqlConnection.Open AccessConnect
// Set sqlRecordset = sqlConnection.Execute("SELECT * FROM
Employees")
sqlConnection.Open(csSQLConnectString,_T(""),_T("" ),0);
sqlRecordset = sqlConnection.Execute(csSQLConnectString,
&vSQLVariant, 0);

However, this doesn't work... Obviously I'm not using the connection
and recordset objects right, but I can't find any documentation on
them.

Any advice?
Thanks in advance.
Nov 13 '08 #2

P: n/a
HumanJHawkins (JH******@Locutius.Com) writes:
I will try ADO.net, but that is probably going to take a bit more
learning... I have never used either .net, or managed C++ before. But
though I forgot to say this in my original post, I am hoping to figure
out how to use ADO (not .net) for now.
Yes, I forgot that you can use ADO from C++ as well. ADO is certainly
more high level than OLE DB, but ADO also does a lot of things behind
your back. Overall, I dislike ADO as an API. ADO .Net is something entirely
different, and a lot better.

I think you are better off with ODBC than ADO.
// In VB it would be:
// sqlConnection.Open AccessConnect
// Set sqlRecordset = sqlConnection.Execute("SELECT * FROM
Employees")
sqlConnection.Open(csSQLConnectString,_T(""),_T("" ),0);
sqlRecordset = sqlConnection.Execute(csSQLConnectString,
&vSQLVariant, 0);

However, this doesn't work... Obviously I'm not using the connection
and recordset objects right, but I can't find any documentation on
them.
In the call to sqlConnection.Execute, shouldn't you pass the query
text, rather then the connection string.

Also, in the connection string:
csSQLConnectString.Format(_T("Provider=MSDATASHAPE ;Data
Provider=SQLOLEDB;")

While MSDATASHAPE maybe useful with ADO, start with a regular
provider like SQLNCLI. SQLNCLI is the provider that ships with
SQL 2005. SQLOLEDB does not give full support for all new
features in SQL 2005.

The same applies to ODBC: use SQL Native Client, not the SQL Server
as the driver.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Nov 13 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.