• Advertisement

Archived

This topic is now archived and is closed to further replies.

SQL connection

This topic is 5032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
does I need to install MSDE or SQL server on my computer to use the SqlConnection object or objects like this?

Share this post


Link to post
Share on other sites
As the documentation says, you need microsoft SQL server for SqlConnection. There''s a similar OleDbConnection that can connect to any Ole DB provider.

Share this post


Link to post
Share on other sites
So to OleDBConnection I don''t need to install anything special on my computer?

and does OLEDB is a good thing to use?

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
Why not using visual c++ for Databases and using C# what is the differences?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by DorRaba
Isn''t C# is the Same language as Visual Basic converted to C?

Sure, that was one of the goals of the language designers, to take Visual Basic and convert it to C. Who would have thought.

Seriously though, no it isn''t. Check it out if you really want to know

Share this post


Link to post
Share on other sites
can you give me a place to start learning the basic stuff of c# in the Internet?

Share this post


Link to post
Share on other sites
There''s a quite good introduction for c++ programmers on code project(the same page I linked to earlier). There''s a lot of other tutorials too, just explore the site.

And of course you shouldn''t forget
MSDN the official documentation.

Share this post


Link to post
Share on other sites
Thanks, Say I have a question,
If I have two forms, Form1 and Form2, how do I show Form2 by clicking a button from Form1?

and another question is how do I declare global variables in C#?

Share this post


Link to post
Share on other sites
First you should read some winforms tutorials, there are plenty of them, both on MSDN and code project.

And there are no true global variables in c#. You can either use the singleton pattern, or use a static member of a class.

Share this post


Link to post
Share on other sites

  • Advertisement