Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Data.OleDb;
Using System.Data;
public class Accesshelper:idisposable
{
Private OleDbConnection m_conn = new OleDbConnection ();
public static string DATABASE = AppDomain.CurrentDomain.BaseDirectory + "rate.accdb";
Public accesshelper ()
{
string connstr = "Provider = microsoft.ace.oledb.12.0; ";
connstr + = "Data Source =\" "+ DATABASE +" \ ""; br> M_conn. ConnectionString = ConnStr;
M_conn. Open ();
}
public void ExecuteCommand (String sql)
{
OleDbCommand cmd = null;
Lock (Cmd=new OleDbCommand ())
{
Cmd. Connection = M_conn;
Cmd.commandtext = SQL;
Cmd. ExecuteNonQuery ();
}
}
public void ExecuteCommand (String sql, oledbparameter[] PS)
{
OleDbCommand cmd = null;
Lock (cmd = new OleDbCommand ())
{
Cmd. Connection = M_conn;
Cmd.commandtext = SQL;
foreach (OleDbParameter p in PS)
{
Cmd. Parameters.Add (P);
}
Cmd. ExecuteNonQuery ();
}
}
Public DataTable GetSource (string strSQL)
{
DataTable dt = null;
OleDbCommand cmd = null;
OleDbDataAdapter ad = null;
Try
{
Lock (dt = new DataTable ())
{
cmd = new OleDbCommand (strSQL, m_conn);
AD = new OleDbDataAdapter ((OleDbCommand) cmd);
Dt. Clear ();
Ad. Fill (DT);
}
}
catch (Exception e)
{
Throw e;
}
return DT;
}
public Object Querysome (String sql)
{
OleDbCommand cmd = null;
Lock (cmd = new OleDbCommand ())
{
Cmd. Connection = M_conn;
Cmd.commandtext = SQL;
using (OleDbDataReader or = cmd. ExecuteReader ())
{
if (or. Read ())
{
Return or. GetValue (0);
}
}
}
return null;
}
public void ExecuteCommand (String sql)
{
OleDbCommand cmd = null;
Lock (cmd=new OleDbCommand ())
{
cmd. Connection = M_conn;
cmd.commandtext = SQL;
cmd. ExecuteNonQuery ();
}
}
void IDisposable.Dispose ()
{
M_conn. Close ();
M_conn. Dispose ();
M_conn = null;
}
}
C # accesshelper complete sample Code