Connect to a Microsoft access database (ADO.NET)

Connect to a Microsoft access database (ADO.NET)

The OleDbConnection object handles the connection to the Microsoft Access Database. The OleDbCommand contains the Microsoft SQL statement that tells the database what to do. The OLE DB connection uses the Microsoft.ACE.OLEDB.12.0, which is the new Access database engine OLE DB driver that can also read previous formats. The Jet OLE DB driver cannot access Microsoft Access 2007 databases. Following example shows connecting with a Microsoft access database.

Example:



using System;
using System.Data.OleDb;

namespace MicrosoftAccessdatabase
{
class Program
{
static void Main(string[] args)
{
string Dbpath = @"..\..\myAccessDB.accdb";
string AccessDbConnectString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Dbpath;

using (OleDbConnection connection = new OleDbConnection(AccessDbConnectString))
{
connection.Open();

Console.WriteLine("Connection State: {0}\n", connection.State);
Console.WriteLine("OLE DB Provider: {0}\n", connection.Provider);
Console.WriteLine("Server Version: {0}\n", connection.ServerVersion);
}
Console.ReadLine();
}
}
}



Output :


AccessDb.bmp






Ebook Download
View all
Learn
View all