Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

C++ command line SQL Database program

Hello,

I need to write a C++ command line program that Inserts, Deletes, Updates
and Selects records from a Microsoft SQL Server Database. I'd really like
to simply open a recrodset like this:

connectStr=_T("ODBC;Provider=MSDASQL;Driver={SQL
Server};Server=192.168.1.2;Database=EOS;Uid=sa;Pwd=sa;");
db.Open(NULL,FALSE,FALSE,connectStr,TRUE);
CRecordset rs(&db);
SQLstring="SELECT * FROM Subscribers WHERE LocationCode='" + buildingStr +
"-" + apartmentStr + "';";
rs.Open(CRecordset::forwardOnly,_T(SQLstring));

The above works great and returns the records. I can movefirst, movenext,
blah, blah. But, I need to be able to Insert, Update and delete like this
too:

CString SQLinsert="INSERT INTO
Changes(LocationCode,Gr
p,Tap,TapType,ChangeDate)VALUES('" + buildingStr +
"-" + apartmentStr + "','" + groupStr + "','" + tapStr + "','" + taptypeStr
+ "','11-10-02');";
rs.Open(CRecordset::dynaset,_T(SQLinsert));

I get an unhandled exception 0xE067363. I know this is a lot to
swallow, but can someone give me some tips or maybe suggest a better way to
do it. I'm very fluent in both C++ and SQL, I'm just stuck. You now how it
is.

Thanks alot,
Eric Kowalewski
0 Kudos
Message 1 of 3
(3,650 Views)
There are a few different ways to insert data into your database via ADO. Off the top of my head, a common way to do INSERTs is to build your INSERT INTO statement as you did in your example, but then pass the statement to the Execute method of your Connection object. Another thing that you could do is call AddNew on the Recordset object, add your values into the current Recordset, then call Update on the Recordset. For some good ADO C++ examples, I recommend that you check out ADO Code Examples in Microsoft Visual C++ on MSDN - it has example C++ code for most of the common ADO properties and methods.

- Elton
0 Kudos
Message 2 of 3
(3,650 Views)
Hi,

Thank You very much. Your tips helped me out a great deal!!!

Eric


"Elton Wells" wrote in message
news:50650000000500000001B40000-1031838699000@exchange.ni.com...
> There are a few different ways to insert data into your database via
> ADO. Off the top of my head, a common way to do INSERTs is to build
> your INSERT INTO statement as you did in your example, but then pass
> the statement to the Execute method of your Connection object.
> Another thing that you could do is call AddNew on the Recordset
> object, add your values into the current Recordset, then call Update
> on the Recordset. For some good ADO C++ examples, I recommend that
> you check out
>
href="http://msdn.microsoft.com/library/default.asp?url=/library/e
n-us/ado27
0/htm/mdmscadocodeexamp...
on MSDN - it has example C++
> code for most of the common ADO properties and methods.
>
> - Elton
0 Kudos
Message 3 of 3
(3,650 Views)