Access Database Example (C#)
[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
* All rights reserved.
*
* Use of this copyright notice is precautionary only, and does not imply
* publication or disclosure. The content of this work contains confidential
* and proprietary information of Simplexar Software Limited. Any duplication,
* modification, distribution, or disclosure in any form, in whole, or in part,
* is strictly prohibited without express prior written permission.
*/
// Get namespaces.
using System;
using System.Data;
using System.Data.OleDb;
using Simplexar.Statsar;
// Start namespace.
namespace Simplexar.Examples.Statsar
{
/// <summary>
/// This example demonstrates how to import data from an ADO.NET data table
/// by connecting to a Microsoft Access database and loading data.
/// </summary>
public static class Example
{
/// <summary>
/// The application entry point.
/// </summary>
public static void Main()
{
try
{
RunExample();
}
catch(Exception exception)
{
Console.WriteLine(exception);
}
Console.WriteLine("Press ENTER to terminate.");
Console.ReadLine();
}
private static void RunExample()
{
// Create a calculator.
StatsCalculator calculator = new StatsCalculator();
// Get data table from database.
DataTable dataTable = GetDataTable();
// Set data table as calculator data source.
calculator.DataSource = dataTable;
DataSheet sheet = calculator.Sheet;
Console.WriteLine(sheet);
}
public static DataTable GetDataTable()
{
// Specify connection string.
string connectionString = string.Format("{0}{1}",
"Provider=Microsoft.Jet.OLEDB.4.0;",
@"Data Source=..\..\..\..\..\Data\AccessDatabase.mdb;");
// Connect and initiate SELECT command.
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand command = new OleDbCommand(
"SELECT * FROM GDP", connection);
// Fill data table.
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
// Dispose managed resources.
adapter.Dispose();
command.Dispose();
connection.Dispose();
return dataTable;
}
}
}