Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   C++.NEt and ADO.NET (http://www.programmingforums.org/showthread.php?t=11083)

hbe02 Aug 15th, 2006 11:47 AM

C++.NEt and ADO.NET
 
The following is source code for executing a query in a c++.NET program that gets data from a "Pubs" database.
:

SqlConnection * cnPubs = new SqlConnection();
// Set the connection string
cnPubs->ConnectionString = S"data source=(local);integrated security=true;initial catalog=Pubs";


        // Try to open the connection
        cnPubs->Open();
                    Console::WriteLine(S"Connected to database successfully!");

        // Create a command object
        SqlCommand * cmTitles = new SqlCommand();
        cmTitles->CommandText = S"SELECT COUNT(*) FROM Titles";
        cmTitles->CommandType = CommandType::Text;
        cmTitles->Connection = cnPubs;Object * numberOfTitles = cmTitles->ExecuteScalar();
       
                        // View results
                        Console::Write(S"Number of titles: ");
        Console::WriteLine(numberOfTitles);

I have not yet done any work with databases. I need to learn how i can create the "Pubs" database which contains a table of records.
I browsed a little on the web and got lost because i dont know what kind of databse is compatible with the code above. all i need to do is a simple task, create the databse and create a table inside it. But what should i use and how will i learn it? is microsoft SQL databse the answer? OLEBD? or ORACLE..? please be specific because i will be using your answers as keywords to look for tutorials.. thanks a bunch :)

Random Spirit Aug 15th, 2006 12:11 PM

May i suggest buying a book on ADO.net.

I Know thats expensive but if you want to learn it properly i would reccomend getting a nice book. It will be way more productive than asking around here or even googleing for answers.

There is also quite a bit of documentation in the MSDN library and the .net 2.0 SDK on ADO.net. Most of examples are in VB.net or C#. That is one of the reasons i hate C++/CLI as the documentation is thin on the ground in certain areas and so you have to have a knowledge of C# or VB to translate them into C++/CLI.

One more thing, if you are getting into .net development i would strongly advide you to use C# for development. C# can do everything C++/CLI can so there is no advantage to using it. The only reason to use C++/CLI is to make seasoned C++ programmers use .net.

It does not matter what database server you use. Microsoft or 3rd parties support most of them on .net.

hbe02 Aug 15th, 2006 12:24 PM

I just finished a book on C++/CLI after learning traditional C++.. and dont plan on getting new books since im starting my fall semester soon. but will difinitely take learnig ADO.NEt into consideration. i will convert to C# next spring.
As for the databse... why cant i simply use MS access for example.. do i need to create a server that handles the SQL conection or just simply create the databse and create the data table, just as MS acces works, then can i simply run my C++/CLI code above?

Random Spirit Aug 15th, 2006 12:33 PM

You can use an MS Access database, you just need the right connection string. So using MS Access OLE DB and OleDbConnection you would use this connection string to a database without a password on a local machine.

Provider=Microsoft.Jet.OLEDB.4.0; Data Source=database.mdb; User Id=admin; Password="

I think thats right, but dont kill me if its slightly wrong. Obviously you have to set it up for your database.

Edit:- i just had a look at the .net 2.0 SDK and i think that should work. If it does not work then post back.

hbe02 Aug 15th, 2006 6:57 PM

waw im soo happy this worked.. thanks a bunch Random Spirit youve been great help..
Few modifications need to be made to the code for it to work:
-Must use following namespace:
:

using namespace System::Data::OleDb;
-And everything that contained sql should be changed to OLEDB:
:

// Create a SqlConnection object
        OleDbConnection * cnPubs = new OleDbConnection();
       
        // Set the connection string
        cnPubs->ConnectionString = S"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Pubs.mdb; User Id=admin; Password=";
       
                //S"data source=(local);integrated security=true;initial catalog=Pubs";
       
       
                // Try to open the connection
                cnPubs->Open();
            Console::WriteLine(S"Connected to database successfully!");

               
                // Create a command object
                OleDbCommand * cmTitles = new OleDbCommand();
                cmTitles->CommandText = S"SELECT COUNT(*) FROM Titles";
                cmTitles->CommandType = CommandType::Text;
                cmTitles->Connection = cnPubs;

                // Execute a SQL statement that returns a scalar value, and display results
                Object * numberOfTitles = cmTitles->ExecuteScalar();
        Console::Write(S"Number of titles: ");
                Console::WriteLine(numberOfTitles);


I concluded that a databse from MSaccess is OLEDB and not an SQL databse... but here are a few questions.. what is the difference between these two? and if i use ORACLE would i have to use an SqlConnection instead of an OledbConnection?

Random Spirit Aug 15th, 2006 7:07 PM

I am glad i could be of help :)

There are various connector objects:

OleDBConnection for Access databases
SqlConnection for SQL Server (this is MS SQL Server)
OdbcConnection for ODBC
OracleConnection for Oracle databases

You use different connection objects to connect to their respective databases. This is just how it works. Different database types need different connector objects.


All times are GMT -5. The time now is 12:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC