using System; using System.Configuration; using System.Data; using System.Data.Odbc; using System.Data.OleDb; using System.Collections; using System.Collections.Generic; using System.Text; namespace DataBase ... { public class DataBase ...{ /**//// <summary> /// 数据库操作类 /// </summary> private string Connstr = null; public DataBase() ...{ Connstr = @ConfigurationSettings.AppSettings["Connstring"]; } /**//// <summary> /// 不使用app.config中的数据库连接符 /// </summary> /// <param name="Str">数据库连接符</param> public DataBase(string Str) ...{ try ...{ this.Connstr = Str; } catch (Exception ex) ...{ throw ex; } } /**//// <summary> /// 返回Connection对象 /// </summary> /// <returns></returns> public OleDbConnection retrunConn() ...{ OleDbConnection Conn = new OleDbConnection(Connstr); Conn.Open(); return Conn; } /**//// <summary> /// 对数据库进行内存释放 /// </summary> /// <param name="Conn"></param> public void Disponse(OleDbConnection Conn) ...{ if (Conn != null) ...{ Conn.Close(); Conn.Dispose(); } GC.Collect(); } /**//// <summary> /// 生成Command对象A,两个参数 /// </summary> /// <param name="Sql"></param> /// <param name="Conn"></param> /// <returns></returns> public OleDbCommand CreateCmd(string Sql, OleDbConnection Conn) ...{ OleDbCommand Cmd; Cmd = new OleDbCommand(Sql, Conn); return Cmd; } /**//// <summary> /// 生成Command对象B,一个参数 /// </summary> /// <param name="Sql"></param> /// <returns></returns> public OleDbCommand CreateCmd(string Sql) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbCommand Cmd; Cmd = new OleDbCommand(Sql, Conn); return Cmd; } /**//// <summary> /// 运行SQL语句 /// </summary> /// <param name="Sql"></param> public void RunProc(string Sql) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbCommand Cmd; Cmd = CreateCmd(Sql, Conn); try ...{ Cmd.ExecuteNonQuery(); } catch ...{ throw new Exception(Sql); } Disponse(Conn); return; } /**//// <summary> /// 运行SQL语句,返回DataReader /// </summary> /// <param name="Sql"></param> /// <returns></returns> public OleDbDataReader RunProcGetReader(string Sql) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbCommand Cmd; Cmd = CreateCmd(Sql, Conn); OleDbDataReader Dr; try ...{ Dr = Cmd.ExecuteReader(CommandBehavior.Default); } catch ...{ throw new Exception(Sql); } return Dr; } /**//// <summary> /// 返回Adapter对象 /// </summary> /// <param name="Sql"></param> /// <returns></returns> public OleDbDataAdapter CreateDa(String Sql) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbDataAdapter Da; Da = new OleDbDataAdapter(Sql, Conn); return Da; } /**//// <summary> /// 运行SQL语句,返回DATASET对象 /// </summary> /// <param name="Sql"></param> /// <param name="Ds"></param> /// <returns></returns> public DataSet RunProc(string Sql, DataSet Ds) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbDataAdapter Da; Da = new OleDbDataAdapter(Sql, Conn); try ...{ Da.Fill(Ds); } catch (Exception err) ...{ throw err; // System.Windows.Forms.MessageBox.Show(err.ToString()); } Disponse(Conn); return Ds; } /**//// <summary> /// 返回指定表名的DataSet /// </summary> /// <param name="Sql"></param> /// <param name="Ds"></param> /// <param name="tableName"></param> /// <returns></returns> public DataSet RunProc(string Sql, DataSet Ds, string tableName) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbDataAdapter Da; Da = CreateDa(Sql); try ...{ Da.Fill(Ds, tableName); } catch (Exception err) ...{ throw err; } Disponse(Conn); return Ds; } /**//// <summary> /// 运行SQL语句,返回指定开始记录数及记录页数及特定表的DATASET /// </summary> /// <param name="Sql"></param> /// <param name="Ds"></param> /// <param name="StartIndex">开始记录号</param> /// <param name="PageSize">页数(条数)</param> /// <param name="TableName"></param> /// <returns></returns> public DataSet RunProc(string Sql, DataSet Ds, int StartIndex, int PageSize, string TableName) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbDataAdapter Da; Da = CreateDa(Sql); try ...{ Da.Fill(Ds, StartIndex, PageSize, TableName); } catch (Exception Err) ...{ throw Err; } Disponse(Conn); return Ds; } /**//// <summary> /// 检验是否存在指定数据 /// </summary> /// <param name="Sql"></param> /// <returns></returns> public bool ExistData(string Sql) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); OleDbDataReader Dr; Dr = CreateCmd(Sql, Conn).ExecuteReader(); if (Dr.Read()) ...{ Disponse(Conn); return true; } else ...{ Disponse(Conn); return false; } } /**//// <summary> /// 返回第一行,第ColumnI列的值 /// </summary> /// <param name="Sql"></param> /// <param name="ColumnI"></param> /// <returns></returns> public string ReturnValue(string Sql, int ColumnI) ...{ OleDbConnection Conn; Conn = new OleDbConnection(Connstr); Conn.Open(); string result; OleDbDataReader Dr; try ...{ Dr = CreateCmd(Sql, Conn).ExecuteReader(); } catch ...{ throw new Exception(Sql); } if (Dr.Read()) ...{ result = Dr[ColumnI].ToString(); } else ...{ result = ""; } Dr.Close(); Disponse(Conn); return result; } }}