473,396 Members | 1,872 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,396 software developers and data experts.

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

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
2 4249
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Papa Legba | last post by:
I'm looking for a Seeking pure CSS horizontal menu with sub-menus which drop down on mouse over. Compatible with as many browsers as possible. Any ideas?
6
by: Greg M | last post by:
I have 5 years of MS Access/VBA development experience and am moving into the VB.net world. I am seeking a tutor which could facilitate this move. I live in Anderson, IN and am willing to...
9
by: RvGrah | last post by:
After much hair-pulling, I've finally found the answer to a problem that many are fighting with, difficulty connecting from Sql 2005 Server Management or VS2005 to a remote Sql Server running Sql...
11
by: billpaterson2006 | last post by:
I've been working on some code to search for specific textstrings and act upon them insome way. I've got the conversion sorted however there is 1 problem remaining. I am trying to work out how...
1
by: Newmanbt | last post by:
All I want to do is create a fairly simple submission form using an objectdatasource, with maybe a drop down list, a radio check list, a textbox, and other pretty straight forward stuff. I cannot...
3
by: dismantle | last post by:
Hello , Just want to ask if anybody here knows a good tutorial website regarding vb.net using ms sql database coz in MS tutorial the knowledge i got there is very limmited and i want to expand my...
3
by: asafok | last post by:
hi , for some reason every time i try connecting to my northwind sql server from VS2005 i get an Error massage. first it was the remote connection that didn't allow me to connect and after that i...
0
by: TonyJ | last post by:
Hello! I use VS2005 and C#. I'm looking at ADO.NET and have found some test tutorial on microsoft MSDN. It can be found on google when searching for "Walkthrough: Saving Data to a Database...
3
by: Blarneystone | last post by:
Ok, I've got a simple access 97 db. named S_tracking.mdb It has two tables 1- Jobs 2- Employees I've set up the references: Imports System.Data.OleDb Imports System.IO Imports System.data
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.