Make connection to database with SqlConnection : SqlConnection « Database ADO.net « Visual C++ .NET






Make connection to database with SqlConnection

 

#include "stdafx.h"


using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Configuration;

void main()
{
    SqlConnection^ connection = gcnew SqlConnection();

  connection->ConnectionString = "SQLConnection";

  try
    {
        connection->Open();
        Console::WriteLine("We got a connection!");
    }
    catch (SqlException ^e)
    {
        Console::WriteLine("No connection the following error occurred: {0}",
            e->Message);
    }
    finally
    {
        connection->Close();
        Console::WriteLine("The connection to the database has been closed");
    }
}

   
  








Related examples in the same category

1.Close database connection in destructor