操作資料庫的類(C#)

來源:互聯網
上載者:User

using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.Reflection;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Runtime.InteropServices ;
using System.Text;
namespace IMS.Class
{
 /// <summary>
 /// LinkDatabase 的摘要說明。
 /// </summary>
 public class LinkDatabase
 {
  private string strSQL="";
  //private string connectionString = "Data Source=Persist Security Info=False;Initial Catalog=;Integrated Security=SSPI";
  private SqlConnection myConnection;
  
  private SqlCommandBuilder sqlCmdBld;
  private DataSet ds = new DataSet();
  private SqlDataAdapter da;

  public string DB_Conn="";
  public LinkDatabase()
  {
   // TODO: 在此處添加建構函式邏輯
   DB_Conn="Persist Security Info=False;Data Source=;Initial Catalog=;User ID=sa;Password=sa";//ConfigurationSettings.AppSettings["ConnStr"];
    }
  public LinkDatabase(string Str)
  {
   try
   {
    this.DB_Conn = Str;
   }
   catch(Exception ex)
   {
    throw ex;
   }
  }
  public bool JudgeServer()
  {
   this.open();
   if(this.myConnection.State==ConnectionState.Open)
   {
    this.myConnection.Close();
    return true;
   }
   else
    return false;
  }
  public void open()
  {
   //調試時若無此句,將使得運行時提示:未將對象引用到執行個體
   this.myConnection=new SqlConnection(this.DB_Conn);
   if(this.myConnection.State==ConnectionState.Open)
   {
    
    return ;
   }
   else
    try
    {
     this.myConnection.Open();
    }
    catch(System.Data.SqlClient.SqlException ex)
    {
     throw new Exception(""+ex.Message +"");
    }
 
  }
  
  /// <summary>
  ///  根據輸入的SQL語句檢索資料庫資料
  /// </summary>
  /// <param name="tempStrSQL">檢索的sql語句</param>
  /// <param name="tempTableName">映射的表名</param>
  /// <returns></returns>
  public DataSet SelectDataBase(string tempStrSQL,string tempTableName)
  {
   this.strSQL = tempStrSQL;
   this.myConnection = new SqlConnection(DB_Conn);
   this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
         this.ds.Clear();
   try
   {
    this.da.Fill(ds,tempTableName);
   }
   catch(Exception e)
   {
    throw new Exception(""+e.Message +"");
   }
   return ds;//返回填充了資料的DataSet,其中資料表以tempTableName給出的字串命名
  }

  /// <summary>
  ///  資料庫資料更新(傳DataSet和DataTable的對象)
  /// </summary>
  /// <param name="changedDataSet">改變了的dataset</param>
  /// <param name="tableName">映射源表的表名</param>
  /// <returns>返回更新了的資料庫表</returns>
  public DataSet UpdateDataBase(DataSet changedDataSet,string tableName,string str)
  {
   try
   {
    this.myConnection = new SqlConnection(DB_Conn);
    this.da = new SqlDataAdapter(str,this.myConnection);
         this.da.SelectCommand=new SqlCommand(str,this.myConnection);
    this.sqlCmdBld = new SqlCommandBuilder(da);
    this.da.Update(changedDataSet,tableName);
    return changedDataSet;//返回更新了的資料庫表
   }
   catch(Exception ex)
   {
               throw new Exception(""+ex.Message +"");
   }      
  }
  /////////////////////////////////  直接操作資料庫(未建立該類的執行個體時直接用)  /////////////////////////////////////////////////////

  /// <summary>
  ///  檢索資料庫資料(傳字串,直接操作資料庫)
  /// </summary>
  /// <param name="tempStrSQL">檢索的sql語句</param>
  /// <returns>查詢的結果,存在於一個datatable中</returns>
  public DataTable SelectDataBase(string tempStrSQL)
  {
   this.myConnection = new SqlConnection(DB_Conn);
   DataSet tempDataSet = new DataSet();
   this.da = new SqlDataAdapter(tempStrSQL,this.myConnection);
   this.da.Fill(tempDataSet);
   return tempDataSet.Tables[0];
  }

  /// <summary>
  ///  資料庫資料更新(傳字串,直接操作資料庫)
  /// </summary>
  /// <param name="tempStrSQL">檢索的sql語句</param>
  /// <returns>返回資料庫中影響的行數</returns>
  public int UpdateDataBase(string tempStrSQL)
  {
   this.myConnection = new SqlConnection(DB_Conn);
   //使用Command之前一定要先開啟串連,後關閉串連,而DataAdapter則會自動開啟關閉串連
   myConnection.Open();
   SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL,this.myConnection);
   int intNumber = tempSqlCommand.ExecuteNonQuery();//返回資料庫中影響的行數
   return intNumber;
  }

  /// <summary>
  ///  關閉資料庫
  /// </summary>
  public void CloseDataBase()
  {
   this.myConnection.Close();
   this.myConnection.Dispose();
   this.ds.Clear();
   this.ds.Dispose();
            GC.Collect();
  }
  /// 返回SQL語句執行結果的第一行第一列
  /// </summary>
  /// <returns>字串</returns>
  public string ReturnValue(string SQL)
  {
   this.myConnection = new SqlConnection(DB_Conn);
   myConnection.Open();
   string result;
   SqlDataReader Dr ;
   try
   {
    SqlCommand Cmd= new SqlCommand(SQL,this.myConnection);
    Dr = Cmd.ExecuteReader();
    if (Dr.Read())
    {
     result = Dr[0].ToString();
     Dr.Close();
    }
    else
    {
     result = "";
     Dr.Close();
    }
   }
   catch
   {
    throw new Exception(SQL);
   }
   Dispose(this.myConnection);
   return result;
  }
  /// 運行預存程序,返回dataset.
  /// </summary>
  /// <param name="procName">預存程序名.</param>
  /// <param name="prams">預存程序入參數組.</param>
  /// <returns>dataset對象.</returns>
  public DataSet RunProc(string procName,SqlParameter[] prams,DataSet Ds)
  {
   this.myConnection = new SqlConnection(DB_Conn);
   myConnection.Open();
   SqlCommand Cmd = new SqlCommand(procName, this.myConnection);
   Cmd.CommandType = CommandType.StoredProcedure;
   if (prams != null)
   {
    foreach (SqlParameter parameter in prams)
    {
     if(parameter != null)
     {
      Cmd.Parameters.Add(parameter);
     }
    }
   } 
   SqlDataAdapter Da = new SqlDataAdapter(Cmd);
   try
   {
    Da.Fill(Ds);
   }
   catch(Exception Ex)
   {
    throw Ex;
   }
   return Ds;
  }
  /// 返回SQL語句第一列,第ColumnI列,
  /// </summary>
  /// <returns>字串</returns>
  public string ReturnValue(string SQL, int ColumnI)
  {
   this.myConnection = new SqlConnection(DB_Conn);
   myConnection.Open();
   string result;
   SqlDataReader Dr ;
   try
   {
    SqlCommand Cmd= new SqlCommand(SQL,this.myConnection);
    Dr = Cmd.ExecuteReader();
   }
   catch
   {
    throw new Exception(SQL);
   }
   if (Dr.Read())
   {
    result = Dr[ColumnI].ToString();
   }
   else
   {
    result = "";
   }
   Dr.Close();
   Dispose(this.myConnection);
   return result;
  }
  public void Dispose(SqlConnection Conn)
  {
   if(Conn!=null)
   {
    Conn.Close();
    Conn.Dispose();
   }
   GC.Collect();
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.