Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 15th, 2006, 11:47 AM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
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
hbe02 is offline   Reply With Quote
Old Aug 15th, 2006, 12:11 PM   #2
Random Spirit
Unverified User
 
Join Date: Aug 2006
Posts: 88
Rep Power: 0 Random Spirit is on a distinguished road
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.
Random Spirit is offline   Reply With Quote
Old Aug 15th, 2006, 12:24 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
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?
hbe02 is offline   Reply With Quote
Old Aug 15th, 2006, 12:33 PM   #4
Random Spirit
Unverified User
 
Join Date: Aug 2006
Posts: 88
Rep Power: 0 Random Spirit is on a distinguished road
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.
Random Spirit is offline   Reply With Quote
Old Aug 15th, 2006, 6:57 PM   #5
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
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?
hbe02 is offline   Reply With Quote
Old Aug 15th, 2006, 7:07 PM   #6
Random Spirit
Unverified User
 
Join Date: Aug 2006
Posts: 88
Rep Power: 0 Random Spirit is on a distinguished road
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.
Random Spirit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO.NET books OpenLoop ASP.NET 1 Jul 18th, 2005 4:43 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:39 PM.

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