面朝大海,春暖华开

focus on scientific computue, 3dgis, spatial database
专注于科学计算、GIS空间分析

 

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;
   }
  }
 }

posted on 2012-12-28 22:07  风过 无痕  阅读(201)  评论(0编辑  收藏  举报

导航

向日葵支付宝收钱码