SQL connection

Started by
14 comments, last by DorRaba 19 years, 11 months ago
Hi, I am trying to create a SQL Database connection and Iv'e created a simple program that just create a SqlConnection Type buy I get an Error when I'm trying to compile it, Can anyone tell me what am I doing wrong? #include "stdafx.h" #using using namespace System; using namespace System::Data; int _tmain() { // TODO: Please replace the sample code below with your own. SqlConnection * conn = NULL; return 0; } I get two errors: 1. c:\Documents and Settings\dor.TYPING\My Documents\Visual Studio Projects\SQLTest\SQLTest.cpp(15): error C2065: 'SqlConnection' : undeclared identifier 2. c:\Documents and Settings\dor.TYPING\My Documents\Visual Studio Projects\SQLTest\SQLTest.cpp(15): error C2065: 'conn' : undeclared identifier Another question is does I need SQL Server installed in my computer to use an SqlConnection Type? [edited by - DorRaba on May 10, 2004 8:53:57 AM]
DDDDor RRRRaba
Advertisement
SqlConnection is in the System::Data::SqlClient namespace, so you either need a using System::Data::SqlClient at the top, or the SqlClient::SqlConnection* conn=NULL;
does I need to install MSDE or SQL server on my computer to use the SqlConnection object or objects like this?
DDDDor RRRRaba
As the documentation says, you need microsoft SQL server for SqlConnection. There''s a similar OleDbConnection that can connect to any Ole DB provider.
So to OleDBConnection I don''t need to install anything special on my computer?

and does OLEDB is a good thing to use?
DDDDor RRRRaba
Yes, you need a database server installed. For testing and learning I recommend Microsoft Access. Because then you can open the database in access and see that your program works correctly. You can also create the required tables from there, instead of needing to do it with sql statements. For a production enviroment you probably need something else, probably MSDE, MySql or postgresql, but in the later cases you alos have to setup the db connection manually which can be messy.

To connect to a access database use the following connection string

"Provider=Microsoft.Jet.OLEDB.4.0; Data source=databasename.mdb"

How do I access to a Access DataBase, Do I need to use OleDB or something else specific?

can anyone give me a simple source code, sample or tutorial?
DDDDor RRRRaba
OleDbConnection* conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source=databasename.mdb");conn->Open();OleDbCommand* command = new OleDbCommand("SELECT * FROM TEST", conn);OleDbDataReader* reader = command->ExecuteReader();while(reader->Read())    Console::WriteLine(reader->GetInt32(0)->ToString() + ", " + reader->GetString(1))reader->Close()conn->Close();


Something like that, there might be errors as it's not tested. This would print the two first coloumns in the table test. Assuming first coloumn is a int and the second is a string.

Addition:
And I don't recommened that you use c++ if you are going to use the .net framework, use c# instead, unless you need c++ for compability. And here's a lot of tutorials about DB programming in c# and .net.

[edited by - fredizzimo on May 10, 2004 10:37:24 AM]

[edited by - fredizzimo on May 10, 2004 10:41:03 AM]
Why not using visual c++ for Databases and using C# what is the differences?
DDDDor RRRRaba
You can use managed c++ with the .net framework, but the code tends to be rather ugly and more complex than it needs to be. The framework is designed for c# and goes much better togheter.

It probably doesn't matter too much for the database part, but at least the winforms part of .net framework in managed c++ is nothing but pain compared to the same thing in c#.

If you already know c++, it doesn't take more than a few hours to get a decent c# programming knowledge, at least good enough for basic programming, so it's very much worth learning.

Edit:
Damn, I always forget somehting This doesn't mean I think c++ is bad, in fact I really love c++, but it's strenght is in different areas, like highly optimized code for example games. I too started programming the .NET framework in c++, but it didn't take long until I realised I was using the wrong language.

[edited by - fredizzimo on May 10, 2004 11:06:15 AM]

This topic is closed to new replies.

Advertisement