Connecting a C++ application to a SQL database

Connecting to a SQL server database with either C# or VB is a straightforward process. However, the support for C++ is relatively very poor. In this post I will provide a walk-through to outline the steps necessary to interact with a SQL database using C++.

When you create a new application using the C# language in Visual Studio there is an option to add a new data source - allowing you to specify a database object. However, if your using C++ it is a completely different story since there is only an option to add a new object source.

After looking around on the net, for information regarding this problem I came across a solution. (Second last post.)

Simply put in Visual Studio (it may be easier to open two instances of Visual Studio):

  1. Create a new Class Library using the C# language, and add a new SQL Server database datasource.

  2. Inside the class define static methods which you will use to open/close and other operations you want to do with your SQL database.

e.g. open database connection method(): returns sqlconnection:


   
   public static SqlConnection openDatabaseConnection()
   {
      try
      {
         sqlconnection = new SqlConnection(Properties.Settings.Default.syndicationConnectionString);
         sqlconnection.Open();
       }
       catch (Exception err)
       {
       }
       return sqlconnection;
   }

3.Compile this Class Library

  1. Now go to your C++ application.

  2. Go to the Solution Explorer view and right click the project name

Click on the References item.

Click on the Add New Reference button, then browse for the .dll file which was created when you compiled the C# Class Library. It should be located in your working directories /bin/debug folder.

So for example its in my C:\dev\testing\cRss\ClassLibrary1\ClassLibrary1\bin\Debug

Click on OK, once you have located the C# .dll file.

Now head over to your header file in your C++ project which you want to interact with the SQL database.

Add the following using statement below is example code:

   

    using RSSDB::RSSDatabaseClass;

Replace the RSSDB with the name of namespace defined earlier in the C# Class Library, and RSSDatabaseClass with the name of the class again defined in the C# Class Library.

Now you can create a new instance of that C# Class Library in your C++ application.

In my header file I defined a new private instance via:

    
    RSSDatabaseClass^ database_connection;
 

Upon compilation you have no errors its safe to assume that things are OK.

The next step is to define function templates in your header file such as openDatabase and closeDatabase.

In the associated .cpp file, simply implement these functions by first creating a new instance of the RSSDatabaseClass in your c++ constructor, then simply calling the methods which you initially define

   
   <span style="font-family:georgia;">Constructor:</span>
   
   database::database(void)
   {
      database_connection = gcnew RSSDatabaseClass();
   }

Function: open database connection on C++ side:

   
   void database::openDatabaseConnection(void)
   {
      try
      {
        sqlconnection= database_connection->openDatabaseConnection();
      }
      catch(Exception ^e)
      {
      }
   }

As can be seen I am calling the openDatabaseConnection() (which returns a sql connection) C# method since this was defined earlier in the C# Class Library.

Debugging remains unchanged.

In the end the following cycle is produced:

C++ Application –> Client application, interacts with the C# Class Library

C# Class Library –> Deals with the database transactions/processes SQL Server database.

SQL Server database –> datasource.

The C# Class Library therefore acts like a proxy class for the C++ application to use.

Last updated on 2 Feb 2009
Published on 2 Feb 2009