C++/CLI Code Snippet - Get Scalar using open connection

C++/CLI Code Snippet - Get Scalar using open connection

C++/CLI Code Snippet - Get Scalar using open connection

C++/CLI code snippet connects to SQL server and executes SQL statement and returns the first column of the first row in the result set returned by the query as a object. ExecuteScalar returns an object using open database connection and SQL statement.

Bookmark:

C++/CLI Code Snippet - Get Scalar using open connection

This .Net C++/CLI code snippet connects to SQL server and executes SQL statement and returns the first column of the first row in the result set returned by the query as a object. Additional columns or rows are ignored. To use this function simply provide open database connection and SQL statement. This function uses SqlClient name space to get data using SqlCommand. Modify the exception handling section to as your project requirements.

System::Object ^ExecuteScalar(System::Data::SqlClient::SqlConnection ^%_SqlConnection, System::String ^_SQL)
{
    System::Object ^_SqlRetVal = nullptr;

    try
    {
        // Executes the query, and returns the first column of the first row in the result 
        // set returned by the query. Additional columns or rows are ignored.
        System::Data::SqlClient::SqlCommand ^_SqlCommand = gcnew System::Data::SqlClient::SqlCommand(_SQL, _SqlConnection);

        _SqlRetVal = _SqlCommand->ExecuteScalar();

        // Dispose command
        delete _SqlCommand;
        _SqlCommand = nullptr;
    }
    catch (Exception ^_Exception)
    {
        // Error occurred while trying to execute reader
        // send error message to console (change below line to customize error handling)
        Console::WriteLine(_Exception->Message);

        return nullptr;
    }

    return _SqlRetVal;
}


Here is a simple example showing how to use above function (ExecuteScalar) to connect to SQL database and get first column of the first row in the result set returned by the query as a object.

// set temporary variable for database connection
System::Data::SqlClient::SqlConnection ^_SqlConnection = gcnew System::Data::SqlClient::SqlConnection();

// assign database connection string
_SqlConnection->ConnectionString = "Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;";

// Connect to database
try
{
    _SqlConnection->Open();
}
catch (Exception ^_Exception)
{
    // Error occurred while trying to connect to database
    Console::WriteLine(_Exception->Message);
}

// Check for valid open database connection before query database
if (_SqlConnection != nullptr && _SqlConnection->State == ConnectionState::Open)
{
    // Lets call above function to execute scalar
    // using open database connection and SQL statement
        // Pass open database connection to function
        // Pass SQL statement to create SqlDataReader
    System::Object ^_object = ExecuteScalar(_SqlConnection, "SELECT name FROM sampletable");

    // Check we have data
    if (_object != nullptr)
    {
        Console::WriteLine(_object->ToString());
    }

    // close database connection
    _SqlConnection->Close();
}


C++/CLI Keywords Used:

  • ExecuteScalar
  • SqlConnection
  • ConnectionString
  • SqlCommand
  • Exception

Code Snippet Information:

  • Applies To: .Net, C++, CLI, SQL, SqlCommand, ExecuteScalar, SQL Server, SQL Client, Connection String, Database Connection, SQL Data Reader
  • Programming Language : C++/CLI

External Resources:

Leave a comment