Search:

Custom Search
_________________________________________________________________

Thursday, April 10, 2008

Connection to a data source in C#

Here is an easy way to get access for example, to an Access database located in your program folder using ADO .NET .

//Add this namespace for database handle
using System.Data.OleDb;

public OleDbConnection GetConnection()
{

OleDbConnection acc = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb";

return acc;

}

Later, when you want to open the database to load, save or whatever you need to do you can use the sentence, being Instance() a method used by Singleton pattern and ConnectionMgr the class name for managing connections:

IDbConnection acc = ConnectionMgr.Instance().GetConnection();

No comments: