C++/CLI Code Snippet - Get Data From SqlDataReader

C++/CLI Code Snippet - Get Data From SqlDataReader

C++/CLI Code Snippet - Get Data From SqlDataReader

C++/CLI code snippet connects to SQL server and executes SQL statement and return SqlDataReader. FunctionSqlDataReader returns SqlDataReader for given database connection string and SQL statement.

Bookmark:

C++/CLI Code Snippet - Get Data From SqlDataReader

This .Net C++/CLI code snippet connect connects to SQL server and executes SQL statement and return SqlDataReader. To use this function simply provide database connection string and SQL statement. This function uses SqlClient name space to get data using SqlDataReader. Modify the exception handling section to as your project requirements. To modify the function to use global database connection, remove the local SqlConnection and make it global, in this way only variable need to pass is SQL statement.

System::Data::SqlClient::SqlDataReader ^GetSqlDataReader(System::String ^_ConnectionString, System::String ^_SQL)
{
    // Set temporary variable to create data reader
    System::Data::SqlClient::SqlDataReader ^_SqlDataReader = nullptr;

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

    // assign database connection string
    _SqlConnection->ConnectionString = _ConnectionString;

    // Connect to database
    try
    {
        _SqlConnection->Open();
    }
    catch (Exception ^_Exception)
    {
        // Error occurred while trying to connect to database
        // send error message to console (change below line to customize error handling)
        Console::WriteLine(_Exception->Message);

        // failed connection, lets return null
        return nullptr;
    }

    // Database connection successful
    // lets run the given SQL statement and get the data to SqlDataReader
    try
    {
        // Pass the connection to a command object
        System::Data::SqlClient::SqlCommand ^_SqlCommand = gcnew System::Data::SqlClient::SqlCommand(_SQL, _SqlConnection);

        // get query results
        _SqlDataReader = _SqlCommand->ExecuteReader();

    }
    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);

        // failed SQL execution, lets return null
        return nullptr;
    }

    // SQL successfully executed, lets return the SqlDataReader
    return _SqlDataReader;
}


Here is a simple example showing how to use above function (GetSqlDataReader) to connect to SQL database and get SqlDataReader for given SQL statement.

// Lets call above function to create a new SqlDataReader
// using given database connection string and SQL statement
    // Pass database connection string to function
    // Pass SQL statement to create SqlDataReader
System::Data::SqlClient::SqlDataReader ^_SqlDataReader = GetSqlDataReader("Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;", "SELECT NAME, PRICE, COST FROM INVENTORY");

// Check we have data in SqlDataReader
if (_SqlDataReader != nullptr && _SqlDataReader->HasRows)
{
    // We have data in SqlDataReader, lets loop through and display the data
    while (_SqlDataReader->Read())
    {
        Console::WriteLine(_SqlDataReader["NAME"]->ToString());
    }

    // Close SqlDataReader
    _SqlDataReader->Close();
    delete _SqlDataReader;
}


C++/CLI Keywords Used:

  • SqlDataReader
  • SqlConnection
  • ConnectionString
  • SqlCommand
  • ExecuteReader
  • Exception
  • nullptr
  • Close
  • delete
  • Console
  • WriteLine

Code Snippet Information:

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

External Resources:

Leave a comment