ACCESS通用操作数据类
public class AccessHelp
{
private string ConnStr;
public Class()
{
ConnStr = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
ConnStr += System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ConfigurationSettings.AppSettings["AccessDB"];
}
public DataSet DataSet(string Tablename, string sqlString, OleDbParameter[] cmdParms)
{
using (OleDbConnection Conn = new OleDbConnection(ConnStr))
{
OpenConnection(Conn);
DataSet ds = new DataSet();
OleDbDataAdapter Adapter = new OleDbDataAdapter(sqlString, Conn);
if(cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
{
Adapter.SelectCommand.Parameters.Add(parm);
}
cmdParms = null;
}
Adapter.Fill(ds,Tablename);
Adapter.Dispose();
Conn.Close();
Conn.Dispose();
return ds;
}
}
public OleDbDataReader ExecuteReader(string sqlString, OleDbParameter[] cmdParms)
{
OleDbConnection Conn = new OleDbConnection(ConnStr);
OpenConnection(Conn);
OleDbCommand cmd = new OleDbCommand(sqlString, Conn);
PrepareCommand(cmd, cmdParms);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public int ExecuteNonQuery(string sqlString, OleDbParameter[] cmdParms)
{
using (OleDbConnection Conn = new OleDbConnection(ConnStr))
{
OpenConnection(Conn);
OleDbCommand cmd = new OleDbCommand(sqlString, Conn);
PrepareCommand(cmd, cmdParms);
int enq = cmd.ExecuteNonQuery();
Conn.Close();
Conn.Dispose();
return enq;
}
}
private void OpenConnection(OleDbConnection Conn)
{
if (Conn.State != ConnectionState.Open)
{
Conn.Open();
}
}
private void PrepareCommand(OleDbCommand cmd, OleDbParameter[] cmdParms)
{
cmd.CommandType = CommandType.Text;
if(cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
cmdParms = null;
}
}
}