VC++ CLR connect to SQL Azure Database

This post shows you how to connect to SQL Azure database using VC++ CLR. In previous post , the code connect to on-premise database. Here we connect it to SQL Azure database. What you need to do is to get the connection string from Azure Portal.

#include “pch.h”

using namespace System;

#using <mscorlib.dll>

using namespace System::Collections;

#using “System.dll”

#using “System.Data.dll”

#using “System.Transactions.dll”

#using “System.EnterpriseServices.dll”

#using “System.Xml.dll”

using namespace System::ComponentModel;

using namespace System::Data;

using namespace System::Data::SqlClient;

int main( array <System:: String ^> ^ args )


// Connect to the SQL Database and issue a SELECT command all in one statement

String ^query = “SELECT * FROM Categories” ;

// build connect string with the userid and password

String ^connectString = String ::Format( “,1433;Initial Catalog=SSISDB;Persist Security Info=False;User ID=username;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;” );

SqlConnection ^ sqlconn = gcnew
SqlConnection (connectString);


SqlCommand ^sqlCommand = gcnew
SqlCommand (query, sqlconn);

// Create a SqlDataReader to enumerate the result of executing the command against the database.

SqlDataReader ^dataReader = sqlCommand->ExecuteReader();

// Find number of columns in result

int numCols = dataReader->FieldCount;

// Display number of columns returned from query

Console ::Write( “No.of columns : “ );

Console ::WriteLine(numCols);

// Display the data, separated by tabs

while (dataReader->Read())


for ( int c = 0; c < numCols – 1; c++)


Console ::Write(dataReader[c]);

Console ::Write( “\t” );


Console ::WriteLine( “” );


return 0;


The result as below.