【tronic】為ASP.NET封裝的SQL資料庫訪問類

iDotNetSpace發表於2008-06-02
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace MyCorporation.DepartMent.DataBase
{
///
/// 通用資料庫類
///

public class DataBase
{

  private string ConnStr = null;

  public DataBase()
  {
   ConnStr = ConfigurationSettings.AppSettings["ConnStr"];
  }
  public DataBase(string Str)
  {
   try
   {
    this.ConnStr = Str;

   }
   catch(Exception ex)
   {
    throw ex;
   }
  }

  ///
  /// 返回connection物件
  ///

  ///
  public SqlConnection ReturnConn()
  {
   SqlConnection Conn = new SqlConnection(ConnStr);
   Conn.Open();
   return Conn;
  }
  public void Dispose(SqlConnection Conn)
  {
   if(Conn!=null)
   {
    Conn.Close();
    Conn.Dispose();
   }
   GC.Collect();
  }
  ///
  /// 執行SQL語句
  ///

  ///
  public void RunProc(string SQL)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlCommand Cmd ;
   Cmd = CreateCmd(SQL, Conn);
   try
   {
    Cmd.ExecuteNonQuery();
   }
   catch
   {
    throw new Exception(SQL);
   }
   Dispose(Conn);
   return;
  }    

  ///
   /// 執行SQL語句返回DataReader
  ///

    ///
    /// SqlDataReader物件.
  public SqlDataReader RunProcGetReader(string SQL)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlCommand Cmd ;
   Cmd = CreateCmd(SQL, Conn);
   SqlDataReader Dr;
   try
   {
    Dr = Cmd.ExecuteReader(CommandBehavior.Default);
   }
   catch
   {
    throw new Exception(SQL);
   }
   //Dispose(Conn);
   return Dr;
  }

  ///
  /// 生成Command物件
  ///

  ///
  ///
  ///
  public SqlCommand CreateCmd(string SQL, SqlConnection  Conn)
  {
   SqlCommand Cmd ;
   Cmd = new SqlCommand(SQL, Conn);
   return Cmd;
  }
  
  ///
  /// 生成Command物件
  ///

  ///
  ///
  public SqlCommand CreateCmd(string SQL)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlCommand Cmd ;
   Cmd = new SqlCommand(SQL, Conn);
   return Cmd;
  }

  ///
  /// 返回adapter物件
  ///

  ///
  ///
  ///
  public SqlDataAdapter CreateDa(string SQL)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlDataAdapter Da;
   Da = new SqlDataAdapter(SQL, Conn);
   return Da;
  }

  ///
  /// 執行SQL語句,返回DataSet物件
  ///

  /// SQL語句
  /// DataSet物件
  public DataSet RunProc(string SQL ,DataSet Ds)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlDataAdapter Da;
   //Da = CreateDa(SQL, Conn);
   Da = new SqlDataAdapter(SQL,Conn);
   try
   {
    Da.Fill(Ds);
   }
   catch(Exception Err)
   {
    throw Err;
   }
   Dispose(Conn);
   return Ds;
  }

  ///
  /// 執行SQL語句,返回DataSet物件
  ///

  /// SQL語句
  /// DataSet物件
  /// 表名
  public DataSet RunProc(string SQL ,DataSet Ds,string tablename)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlDataAdapter Da;
   Da = CreateDa(SQL);
   try
   {
    Da.Fill(Ds,tablename);
   }
   catch(Exception Ex)
   {
    throw Ex;
   }
   Dispose(Conn);
   return Ds;
  }

  ///
  /// 執行SQL語句,返回DataSet物件
  ///

  /// SQL語句
  /// DataSet物件
  /// 表名
  public DataSet RunProc(string SQL , DataSet Ds ,int  StartIndex ,int PageSize, string tablename )
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlDataAdapter Da ;
   Da = CreateDa(SQL);
   try
   {
    Da.Fill(Ds, StartIndex, PageSize, tablename);
   }
   catch(Exception Ex)
   {
    throw Ex;
   }
   Dispose(Conn);
   return Ds;
  }

  ///
  /// 檢驗是否存在資料
  ///

  ///
  public bool ExistDate(string SQL)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlDataReader Dr ;
   Dr = CreateCmd(SQL,Conn).ExecuteReader();
   if (Dr.Read())
   {
    Dispose(Conn);
    return true;
   }
   else
   {
    Dispose(Conn);
    return false;
   }
  }

  ///
  /// 返回SQL語句執行結果的第一行第一列
  ///

  /// 字串
  public string ReturnValue(string SQL)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   string result;
   SqlDataReader Dr ;
   try
   {
    Dr = CreateCmd(SQL,Conn).ExecuteReader();
    if (Dr.Read())
    {
     result = Dr[0].ToString();
     Dr.Close();
    }
    else
    {
     result = "";
     Dr.Close();
    }
   }
   catch
   {
    throw new Exception(SQL);
   }
   Dispose(Conn);
   return result;
  }

  ///
  /// 返回SQL語句第一列,第ColumnI列,
  ///

  /// 字串
  public string ReturnValue(string SQL, int ColumnI)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   string result;
   SqlDataReader Dr ;
   try
   {
    Dr = CreateCmd(SQL,Conn).ExecuteReader();
   }
   catch
   {
    throw new Exception(SQL);
   }
   if (Dr.Read())
   {
    result = Dr[ColumnI].ToString();
   }
   else
   {
    result = "";
   }
   Dr.Close();
   Dispose(Conn);
   return result;
  }

  ///
  /// 生成一個儲存過程使用的sqlcommand.
  ///

  /// 儲存過程名.
  /// 儲存過程入引數組.
  /// sqlcommand物件.
  public SqlCommand CreateCmd(string procName, SqlParameter[] prams)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlCommand Cmd = new SqlCommand(procName, Conn);
   Cmd.CommandType = CommandType.StoredProcedure;
   if (prams != null)
   {
    foreach (SqlParameter parameter in prams)
    {
     if(parameter != null)
     {
      Cmd.Parameters.Add(parameter);
     }
    }
   }
   return Cmd;
  }

  ///
  /// 為儲存過程生成一個SqlCommand物件
  ///

  /// 儲存過程名
  /// 儲存過程引數
  /// SqlCommand物件
  private SqlCommand CreateCmd(string procName, SqlParameter[] prams,SqlDataReader Dr)
  {
   SqlConnection Conn;
   Conn = new SqlConnection(ConnStr);
   Conn.Open();
   SqlCommand Cmd = new SqlCommand(procName, Conn);
   Cmd.CommandType = CommandType.StoredProcedure;
   if (prams != null)
   {
    foreach (SqlParameter parameter in prams)
     Cmd.Parameters.Add(parameter);
   }
   Cmd.Parameters.Add(
    new SqlParameter("ReturnValue", SqlDbType.Int, 4,
    ParameterDirection.ReturnValue, false, 0, 0,
    string.Empty, DataRowVersion.Default, null));

   return Cmd;
  }

  ///
  /// 執行儲存過程,返回.
  ///

  /// 儲存過程名
  /// 儲存過程引數
  /// SqlDataReader物件
  public void RunProc(string procName, SqlParameter[] prams, SqlDataReader Dr)
  {

   SqlCommand Cmd = CreateCmd(procName, prams, Dr);
   Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
   return;
  }

  ///
  /// 執行儲存過程,返回.
  ///

  /// 儲存過程名
  /// 儲存過程引數
  public string RunProc(string procName, SqlParameter[] prams)
  {
   SqlDataReader Dr;
   SqlCommand Cmd = CreateCmd(procName, prams);
   Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
   if(Dr.Read())
   {
    return Dr.GetValue(0).ToString();
   }
   else
   {
    return "";
   }
  }

  ///
  /// 執行儲存過程,返回dataset.
  ///

  /// 儲存過程名.
  /// 儲存過程入引數組.
  /// dataset物件.
  public DataSet RunProc(string procName,SqlParameter[] prams,DataSet Ds)
  {
   SqlCommand Cmd = CreateCmd(procName,prams);
   SqlDataAdapter Da = new SqlDataAdapter(Cmd);
   try
   {
    Da.Fill(Ds);
   }
   catch(Exception Ex)
   {
    throw Ex;
   }
   return Ds;
  }

}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-329675/,如需轉載,請註明出處,否則將追究法律責任。

相關文章