C# OLEDB Connection

The C# OleDbConnection instance takes Connection String as argument and pass the value to the Constructor statement. An instance of the C# OleDbConnection class is supported the OLEDB Data Provider .

connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=yourdatabasename.mdb;"; cnn = new OleDbConnection(connetionString);

When the connection is established between C# application and the specified Data Source, SQL Commands will execute with the help of the Connection Object and retrieve or manipulate data in the database. Once the Database activities is over Connection should be closed and release from the data source resources .

cnn.Close();

The Close() method in the OleDbConnection class is used to close the Database Connection. The Close method Rolls Back any pending transactions and releases the Connection from the Database connected by the OLEDB Data Provider.

Full Source C#
using System; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; OleDbConnection cnn ; connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;"; cnn = new OleDbConnection(connetionString); try { cnn.Open(); MessageBox.Show ("Connection Open ! "); cnn.Close(); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }