Connecting To Access
using System;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
OleDbConnection MyConnection = new OleDbConnection (@"Provider=Microsft.Jet.OLEDB.4.0; Data Source = c:\Northwind.mdb");
MyConnection.Open();
}
}
Connecting to a Microsoft Access Database
using System;
using System.Data;
using System.Data.OleDb;
class Program
{
static void Main(string[] args)
{
string oledbConnectString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Northwind2007.accdb;";
using (OleDbConnection connection = new OleDbConnection(oledbConnectString))
{
connection.Open( );
Console.WriteLine("Connection State: {0}", connection.State);
Console.WriteLine("OLE DB Provider: {0}", connection.Provider);
Console.WriteLine("Server Version: {0}", connection.ServerVersion);
}
}
}
Connecting to a Password-Protected Microsoft Access Database
using System;
using System.Data;
using System.Data.OleDb;
class Program
{
static void Main(string[] args)
{
string oledbConnectString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Northwind.accdb;" +
"Jet OLEDB:Database Password=password;";
using (OleDbConnection connection = new OleDbConnection(oledbConnectString))
{
connection.Open( );
Console.WriteLine("Connection State: {0}", connection.State);
Console.WriteLine("OLE DB Provider: {0}", connection.Provider);
Console.WriteLine("Server Version: {0}", connection.ServerVersion);
}
}
}
Read data from Access database
using System;
using System.Data;
using System.Data.rumon;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
string connectionString = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\Northwind.mdb";
OleDbConnection conn = new OleDbConnection(connectionString);
string sql = "SELECT * FROM Orders";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.Write(reader.GetString(0).ToString() + " ," );
Console.Write(reader.GetString(1).ToString() + " ," );
Console.WriteLine("");
}
reader.Close();
conn.Close();
}
}
use an OleDbCommand object to read data from Access database
using System;
using System.Data;
using System.Data.OleDb;
class OleDbConnectionAccess {
public static void Main() {
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=F:\\Program Files\\MicrosoftOffice\\Office\\Samples\\Northwind.mdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbCommand.rumandText = "SELECT CustomerID, CompanyName, ContactName, Address FROM Customers WHERE CustomerID = "ALFKI"";
myOleDbConnection.Open();
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();
Console.WriteLine("myOleDbDataReader[\" CustomerID\"] = " + myOleDbDataReader["CustomerID"]);
Console.WriteLine("myOleDbDataReader[\" CompanyName\"] = " + myOleDbDataReader["CompanyName"]);
Console.WriteLine("myOleDbDataReader[\" ContactName\"] = " + myOleDbDataReader["ContactName"]);
Console.WriteLine("myOleDbDataReader[\" Address\"] = " + myOleDbDataReader["Address"]);
myOleDbDataReader.Close();
myOleDbConnection.Close();
}
}
Use OleDbCommand to load data from Access database mdb
using System;
using System.Data;
using System.Data.OleDb;
class MainClass
{
[STAThread]
static void Main(string[] args)
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Northwind.mdb";
string SQL = "SELECT * FROM Orders";
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand(SQL);
cmd.Connection = conn;
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.Write("OrderID:"+reader.GetInt32(0).ToString() );
Console.Write(" ,");
Console.WriteLine("Customer:" + reader.GetString(1).ToString() );
}
reader.Close();
conn.Close();
}
}
OrderID:10330 ,Customer:LILAS
OrderID:10331 ,Customer:BONAP
OrderID:10332 ,Customer:MEREP
OrderID:10333 ,Customer:WARTH
OrderID:10334 ,Customer:VICTE
...
...