locked
Coonecting to SQL using C++ RRS feed

  • Question

  • Hi can some one please explain to me how to use C++ .net to connect to an SQL server PLEASE PLEASE!! because in msdn i have seen code like this 

    SqlConnection* myConnection = new SqlConnection();

    myConnection->ConnectionString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30";
    myConnection->Open();
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.71).aspx
    but it gives me a compile error which states that System::Data::SQLClient does not exist.

    I am using Visual Studio 2010

    Thanks a lot for your time.


    Noel Farrugia
    Sunday, September 4, 2011 8:46 PM

Answers

  • Just to get you started. This code compiles, but you have to manage connection string and SqlConnection itself to actually connect to database:

     

     

    #using <System.dll>
    
    #using <System.Data.dll>
    
    
    using namespace System;
    
    using namespace System::Data;
    
    using namespace System::Data::Sql;
    
    using namespace System::Data::SqlClient;
    
    int main(array<System::String ^> ^args)
    
    {
    
        SqlConnection^ myConnection = gcnew SqlConnection();
    
        myConnection->ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    
        myConnection->Open();
    
        return 0;
    
    }
    

    • Edited by Sergey Chepurin Monday, September 5, 2011 9:44 AM
    • Marked as answer by lucy-liu Friday, September 16, 2011 6:06 AM
    Monday, September 5, 2011 9:30 AM
  • So when creating a C++ project in Visual Studio how can you set it to managed C++ or it detects it automatically. Thanks alot for your time :)
    Noel Farrugia

    In Wizard you select New->Project->CLR. It creates new C++/CLI project.

    You also can mix C++ and managed C++ selecting Project->properties->Configuration properties->General->Common Language Rutime Support.

    All these projects require .NET framework when native C++ does not.


    • Edited by Sergey Chepurin Monday, September 5, 2011 10:13 AM
    • Marked as answer by lucy-liu Friday, September 16, 2011 6:06 AM
    Monday, September 5, 2011 10:09 AM
  • Because C++ is more powerful as a programming language than C#...

    For database applications, I strongly disagree with you.

    The Entity Framework technology is extremely powerful and valuable for database design, and LINQ is extraordinarily useful. Neither of these technologies is available to C++, but is available to C#. You are missing out on them by choosing C++.

    • Marked as answer by lucy-liu Friday, September 16, 2011 6:05 AM
    Monday, September 5, 2011 4:25 PM

All replies

  • Is your application managed or unmanaged?

    For unmanaged applications, I suggest using ADO. You can read more about it here.

    Sunday, September 4, 2011 10:13 PM
  • I would like to create a project with managed C++ i.e using .NET Framework 4, could you suggest on how to create a visual studio 2010 project. Thanks a lot for your time.
    Noel Farrugia
    Monday, September 5, 2011 5:54 AM
  • why ADO and not ODBC ?
    yo
    Monday, September 5, 2011 6:31 AM
  • for me it does not make a difference which one to use, but i saw on msdn that with managed C++ using .net was much simpler but it gave me a compile error when implementing the reference to SQLClient. I would like someone to tell me if possible how to create a C++ project using .net framework 4 using visual studio 2010
    Noel Farrugia
    Monday, September 5, 2011 6:36 AM
  • [...]


    but it gives me a compile error which states that System::Data::SQLClient does not exist.

    [...]

    Try the correct name of namespace: SqlClient.

    • Edited by Viorel_MVP Monday, September 5, 2011 7:26 AM
    Monday, September 5, 2011 7:25 AM
  • I tried that to, my problem is i think i am not creating a C++ .net framework project how can i check pls?
    Noel Farrugia
    Monday, September 5, 2011 7:27 AM
  • Is it possible for you to paste your code here?

    Apart from this,please check the reference for System.Data.SqlClient. Have you used it in your code?

     

     


    Cheers!!!

    Vatsa

    www.objectiveprogramming.com

     

    • Edited by vatsa_mitr Monday, September 5, 2011 7:29 AM
    Monday, September 5, 2011 7:29 AM
  • Just to get you started. This code compiles, but you have to manage connection string and SqlConnection itself to actually connect to database:

     

     

    #using <System.dll>
    
    #using <System.Data.dll>
    
    
    using namespace System;
    
    using namespace System::Data;
    
    using namespace System::Data::Sql;
    
    using namespace System::Data::SqlClient;
    
    int main(array<System::String ^> ^args)
    
    {
    
        SqlConnection^ myConnection = gcnew SqlConnection();
    
        myConnection->ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    
        myConnection->Open();
    
        return 0;
    
    }
    

    • Edited by Sergey Chepurin Monday, September 5, 2011 9:44 AM
    • Marked as answer by lucy-liu Friday, September 16, 2011 6:06 AM
    Monday, September 5, 2011 9:30 AM
  • i'll try this out and let you know. Thanks alot :D :D
    Noel Farrugia
    Monday, September 5, 2011 9:38 AM
  • i'll try this out and let you know. Thanks alot :D :D
    Noel Farrugia
    I just want to add that managed C++ is another language - not C++. It is even closer to C#, in my opinion. They share the same classes (namespaces) and only syntax somehow reminds C++. So, do not expect a lot of help from this C++ General Forum.
    Monday, September 5, 2011 9:42 AM
  • So when creating a C++ project in Visual Studio how can you set it to managed C++ or it detects it automatically. Thanks alot for your time :)
    Noel Farrugia
    Monday, September 5, 2011 9:51 AM
  • So when creating a C++ project in Visual Studio how can you set it to managed C++ or it detects it automatically. Thanks alot for your time :)
    Noel Farrugia

    In Wizard you select New->Project->CLR. It creates new C++/CLI project.

    You also can mix C++ and managed C++ selecting Project->properties->Configuration properties->General->Common Language Rutime Support.

    All these projects require .NET framework when native C++ does not.


    • Edited by Sergey Chepurin Monday, September 5, 2011 10:13 AM
    • Marked as answer by lucy-liu Friday, September 16, 2011 6:06 AM
    Monday, September 5, 2011 10:09 AM
  • cool, thanks alot =]
    Noel Farrugia
    Monday, September 5, 2011 10:15 AM
  • why ADO and not ODBC ?
     ADO is a wrapper above both ODBC and OLEDB, and can be used for either protocol. 
    Monday, September 5, 2011 3:50 PM
  • cool, thanks alot =]

    In my opinion, if you are thinking of using SQL Server, your application is more likely to fall into the category where C# should be considered for client code development. Database applications can be developed under C# much faster, and the tools are simply much better.

    Is there a reason you are choosing C++ over C#?

    Monday, September 5, 2011 3:52 PM
  • Because C++ is more powerful as a programming language than C#, i have worked with C# aswell but i would like the freedom of pointers etc. :)
    Noel Farrugia
    Monday, September 5, 2011 3:59 PM
  • Because C++ is more powerful as a programming language than C#...

    For database applications, I strongly disagree with you.

    The Entity Framework technology is extremely powerful and valuable for database design, and LINQ is extraordinarily useful. Neither of these technologies is available to C++, but is available to C#. You are missing out on them by choosing C++.

    • Marked as answer by lucy-liu Friday, September 16, 2011 6:05 AM
    Monday, September 5, 2011 4:25 PM
  • I'll do it with C# then because C++ makes it a lot more complex . Thanks for your time
    Noel Farrugia
    Monday, September 5, 2011 4:30 PM