Database.cs
上传用户:szraylite
上传日期:2018-06-06
资源大小:11546k
文件大小:8k
源码类别:

软件测试

开发平台:

Java

  1. using System;
  2. using System.ComponentModel;
  3. using System.Collections;
  4. using System.Diagnostics;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.Configuration;
  8. using System.Web;
  9. namespace ENTERPRISE.DataAccessLayer
  10. {
  11.     /// <summary>
  12.     /// Database 的摘要说明
  13.     /// </summary>
  14.     /// 
  15.     public class Database : IDisposable
  16.     {
  17.         /// <summary>
  18.         /// 保护变量,数据库连接。
  19.         /// </summary>
  20.         protected OleDbConnection Connection;
  21.         /// <summary>
  22.         /// 保护变量,数据库连接串。
  23.         /// </summary>
  24.         public String ConnectionString;
  25.         /// <summary>
  26.         /// 构造函数。
  27.         /// </summary>
  28.         /// <param name="DatabaseConnectionString">数据库连接串</param>
  29.         public Database()
  30.         {
  31.             System.Web.UI.Page page = new System.Web.UI.Page();
  32.             ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + page.Server.MapPath(ConfigurationManager.ConnectionStrings["AccessFileName"].ConnectionString);
  33.         }
  34.         /// <summary>
  35.         /// 构造函数。
  36.         /// </summary>
  37.         /// <param name="pDatabaseConnectionString">数据库连接串</param>
  38.         public Database(string pDatabaseConnectionString)
  39.         {
  40.             ConnectionString = pDatabaseConnectionString;
  41.         }
  42.         /// <summary>
  43.         /// 析构函数,释放非托管资源
  44.         /// </summary>
  45.         ~Database()
  46.         {
  47.             try
  48.             {
  49.                 if (Connection != null)
  50.                     Connection.Close();
  51.             }
  52.             catch { }
  53.             try
  54.             {
  55.                 Dispose();
  56.             }
  57.             catch { }
  58.         }
  59.         /// <summary>
  60.         /// 保护方法,打开数据库连接。
  61.         /// </summary>
  62.         protected void Open()
  63.         {
  64.             if (Connection == null)
  65.             {
  66.                 Connection = new OleDbConnection(ConnectionString);
  67.             }
  68.             if (Connection.State.Equals(ConnectionState.Closed))
  69.             {
  70.                 Connection.Open();
  71.             }
  72.         }
  73.         /// <summary>
  74.         /// 公有方法,关闭数据库连接。
  75.         /// </summary>
  76.         public void Close()
  77.         {
  78.             if (Connection != null)
  79.                 Connection.Close();
  80.         }
  81.         /// <summary>
  82.         /// 公有方法,释放资源。
  83.         /// </summary>
  84.         public void Dispose()
  85.         {
  86.             // 确保连接被关闭
  87.             if (Connection != null)
  88.             {
  89.                 Connection.Dispose();
  90.                 Connection = null;
  91.             }
  92.         }
  93.         /// <summary>
  94.         /// 公有方法,获取数据,返回一个SqlDataReader (调用后主意调用SqlDataReader.Close())。
  95.         /// </summary>
  96.         /// <param name="SqlString">Sql语句</param>
  97.         /// <returns>SqlDataReader</returns>
  98.         public OleDbDataReader GetDataReader(String SqlString)
  99.         {
  100.             Open();
  101.             OleDbCommand cmd = new OleDbCommand(SqlString, Connection);
  102.             return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
  103.         }
  104.         /// <summary>
  105.         /// 公有方法,获取数据,返回一个DataSet。
  106.         /// </summary>
  107.         /// <param name="SqlString">Sql语句</param>
  108.         /// <returns>DataSet</returns>
  109.         public DataSet GetDataSet(String SqlString)
  110.         {
  111.             Open();
  112.             OleDbDataAdapter adapter = new OleDbDataAdapter(SqlString, Connection);
  113.             DataSet dataset = new DataSet();
  114.             adapter.Fill(dataset);
  115.             Close();
  116.             return dataset;
  117.         }
  118.         /// <summary>
  119.         /// 公有方法,获取数据,返回一个DataRow。
  120.         /// </summary>
  121.         /// <param name="SqlString">Sql语句</param>
  122.         /// <returns>DataRow</returns>
  123.         public DataRow GetDataRow(String SqlString)
  124.         {
  125.             DataSet dataset = GetDataSet(SqlString);
  126.             dataset.CaseSensitive = false;
  127.             if (dataset.Tables[0].Rows.Count > 0)
  128.             {
  129.                 return dataset.Tables[0].Rows[0];
  130.             }
  131.             else
  132.             {
  133.                 return null;
  134.             }
  135.         }
  136.         /// <summary>
  137.         /// 公有方法,执行Sql语句。
  138.         /// </summary>
  139.         /// <param name="SqlString">Sql语句</param>
  140.         /// <returns>对Update、Insert、Delete为影响到的行数,其他情况为-1</returns>
  141.         public int ExecuteSQL(String SqlString)
  142.         {
  143.             int count = -1;
  144.             Open();
  145.             try
  146.             {
  147.                 OleDbCommand cmd = new OleDbCommand(SqlString, Connection);
  148.                 count = cmd.ExecuteNonQuery();
  149.             }
  150.             catch
  151.             {
  152.                 count = -1;
  153.             }
  154.             finally
  155.             {
  156.                 Close();
  157.             }
  158.             return count;
  159.         }
  160.         /// <summary>
  161.         /// 公有方法,执行一组Sql语句。
  162.         /// </summary>
  163.         /// <param name="SqlStrings">Sql语句组</param>
  164.         /// <returns>是否成功</returns>
  165.         public bool ExecuteSQL(ArrayList SqlStrings)
  166.         {
  167.             bool success = true;
  168.             Open();
  169.             OleDbCommand cmd = new OleDbCommand();
  170.             OleDbTransaction trans = Connection.BeginTransaction();
  171.             cmd.Connection = Connection;
  172.             cmd.Transaction = trans;
  173.             try
  174.             {
  175.                 foreach (String str in SqlStrings)
  176.                 {
  177.                     cmd.CommandText = str;
  178.                     cmd.ExecuteNonQuery();
  179.                 }
  180.                 trans.Commit();
  181.             }
  182.             catch
  183.             {
  184.                 success = false;
  185.                 trans.Rollback();
  186.             }
  187.             finally
  188.             {
  189.                 Close();
  190.             }
  191.             return success;
  192.         }
  193.         /// <summary>
  194.         /// 公有方法,在一个数据表中插入一条记录。
  195.         /// </summary>
  196.         /// <param name="TableName">表名</param>
  197.         /// <param name="Cols">哈西表,键值为字段名,值为字段值</param>
  198.         /// <returns>是否成功</returns>
  199.         public bool Insert(String TableName, NoSortHashtable Cols)
  200.         {
  201.             int Count = 0;
  202.            
  203.             if (Cols.Count <= 0)
  204.             {
  205.                 return true;
  206.             }
  207.             String Fields = " (";
  208.             String Values = " Values(";
  209.             foreach (string str in Cols.Keys)
  210.             {
  211.                 if (Count != 0)
  212.                 {
  213.                     Fields += ",";
  214.                     Values += ",";
  215.                 }
  216.                 Fields += str;
  217.                 Values += Cols[str];
  218.                 Count++;
  219.             }
  220.             Fields += ")";
  221.             Values += ")";
  222.            String SqlString = "Insert into " + TableName + Fields + Values;
  223.            return Convert.ToBoolean(ExecuteSQL(SqlString));
  224.         }
  225.         /// <summary>
  226.         /// 公有方法,更新一个数据表。
  227.         /// </summary>
  228.         /// <param name="TableName">表名</param>
  229.         /// <param name="Cols">哈西表,键值为字段名,值为字段值</param>
  230.         /// <param name="Where">Where子句</param>
  231.         /// <returns>是否成功</returns>
  232.         public bool Update(String TableName, Hashtable Cols, String Where)
  233.         {
  234.             int Count = 0;
  235.             if (Cols.Count <= 0)
  236.             {
  237.                 return true;
  238.             }
  239.             String Fields = " ";
  240.             foreach (DictionaryEntry item in Cols)
  241.             {
  242.                 if (Count != 0)
  243.                 {
  244.                     Fields += ",";
  245.                 }
  246.                 Fields += item.Key.ToString();
  247.                 Fields += "=";
  248.                 Fields += item.Value.ToString();
  249.                 Count++;
  250.             }
  251.             Fields += " ";
  252.             String SqlString = "Update " + TableName + " Set " + Fields + Where;
  253.             return Convert.ToBoolean(ExecuteSQL(SqlString));
  254.         }
  255.     }
  256. }