Simple Data Display



When dealing with a database in a console application, you may want to display the records on a DOS window. To do this, you can use a DataSet object.

The DataSet class allows you to access any type of information from a table. These include table's object name, the columns (and their properties), and the records. This means that you should be able to locate a record, retrieve its value, and pass it to the Console::Write() or the Console::WriteLine() methods. Probably the only real problem is to make sure your DataSet object can get the necessary records. The records could come from a database (Microsoft SQL Server, Oracle, Microsoft Access, Paradox, etc).

Here is an example of displaying data on a console from the records of a Microsoft SQL Server table:

// Exercise.cpp : main project file.

#include "stdafx.h"

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

int main(array<System::String ^> ^args)
    SqlConnection ^ conDatabase =
            gcnew SqlConnection(L"Data Source=(local);Database='bcr1';"
                L"Integrated Security=true");
    SqlCommand ^ cmdDatabase =
        gcnew SqlCommand(L"SELECT * FROM dbo.Employees;", conDatabase);

    DataSet ^ dsEmployees = gcnew DataSet("EmployeesSet");
    SqlDataAdapter ^ sda = gcnew SqlDataAdapter();
    sda->SelectCommand = cmdDatabase;

    DataRow ^ recEmployee = dsEmployees->Tables[0]->Rows[0];

    Console::WriteLine(L"First Name: {0}", 
		dynamic_cast<String ^>(recEmployee[L"FirstName"]));
    Console::WriteLine(L"Last Name:  {0}", 
		dynamic_cast<String ^>(recEmployee[L"LastName"]));


    return 0;

Home Copyright © 2007-2013, FunctionX