using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data; namespace DataAccess { public class DataBasic { private string cnStr; private OleDbConnection cn; public DataBasic() { cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "/????.mdb;Jet OLEDB:Database Password='????'"; cn = null; } ~DataBasic() { try { if (cn != null) { cn.Close(); cn.Dispose(); } } catch { } finally { cn.Close(); cn.Dispose(); } } //打开数据库连接 protected void Open() { if (cn == null) { cn = new OleDbConnection(cnStr); } if (cn.State == System.Data.ConnectionState.Closed) { cn.Open(); } } //关闭数据库连接 protected void Close() { if (cn != null) { cn.Close(); } } //执行命令操作,以sql字符串为参数传递 public void Execute(string sql) { this.Open(); OleDbCommand cmd = new OleDbCommand(sql,cn); cmd.ExecuteNonQuery(); this.Close(); } //返回数据集 public DataSet GetDataSet(string sql) { this.Open(); OleDbCommand cmd = new OleDbCommand(sql,cn); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = cmd; DataSet dataset = new DataSet(); adapter.Fill(dataset); this.Close(); return dataset; } } }