using System; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; namespace OperateDatabase_pro { ///<summary> /// 本程式為通用資料庫通路操作類,可以對SQL SERVER和Access的通路。 /// 後期版本正在制作當中,将及時更新。 /// /// *** 後期版本對此版進行了優化和擴充,大家可以下載下傳對比,大家下載下傳此源碼隻能僅供交流學習參考之 用*** /// /// 作者:cnetstart(吳健) /// Email :[email protected] QQ:358122678 /// 網址:http://blog.csdn.net/cnetstar/ /// ★★★★★您可以免費使用此程式,但是請您完整保留此說明,以維護知識産權★★★★★ /// ///</summary> public class OperateDatabase { private System.String m_ConnectionString; private System.Boolean m_IsSQLServer; private SqlConnection m_SqlConn=new SqlConnection(); private OleDbConnection m_DbConn=new OleDbConnection(); public OperateDatabase(System.String connectionString,System.Boolean isSQLServer) { this.m_ConnectionString=connectionString; this.m_IsSQLServer=isSQLServer; } ~OperateDatabase() { this.m_SqlConn.Dispose(); this.m_DbConn.Dispose(); } private void OpenDatabase() { try { if(this.m_IsSQLServer) { if(this.m_SqlConn.State==ConnectionState.Closed) { this.m_SqlConn.ConnectionString=this.m_ConnectionString; this.m_SqlConn.Open(); } } else { if(this.m_DbConn.State==ConnectionState.Closed) { this.m_DbConn.ConnectionString=this.m_ConnectionString; this.m_DbConn.Open(); } } } catch(Exception ex) { throw new ArgumentException(ex.Message); } } private void CloseDatabase() { try { if(this.m_IsSQLServer) { if(this.m_SqlConn.State!=ConnectionState.Closed) { this.m_SqlConn.Close(); } } else { if(this.m_DbConn.State!=ConnectionState.Closed) { this.m_DbConn.Close(); } } } catch(Exception ex) { throw new ArgumentException(ex.Message); } } //for sqlclient and oledb public int ExecuteNonquery(System.String sql,System.Boolean isStoredProcedure,params System.Object[] parameters) { int iResult=-1; try { if(this.m_IsSQLServer) { SqlCommand cmd=new SqlCommand(sql,this.m_SqlConn); foreach(System.Object obj in parameters) { cmd.Parameters.Add((SqlParameter)obj); } if(isStoredProcedure) { cmd.CommandType=CommandType.StoredProcedure; } this.OpenDatabase(); iResult=cmd.ExecuteNonQuery(); cmd.Dispose(); this.CloseDatabase(); } else { OleDbCommand cmd=new OleDbCommand(sql,this.m_DbConn); foreach(System.Object obj in parameters) { cmd.Parameters.Add((OleDbParameter)obj); } if(isStoredProcedure) { cmd.CommandType=CommandType.StoredProcedure; } this.OpenDatabase(); iResult=cmd.ExecuteNonQuery(); cmd.Dispose(); this.CloseDatabase(); } } catch(Exception ex) { throw new ArgumentException(ex.Message); } return iResult; } //for sqlclient only public void ExecuteNonquery(System.String[] sql) { this.OpenDatabase(); SqlTransaction trans=this.m_SqlConn.BeginTransaction("trans1"); SqlCommand cmd=new SqlCommand(); cmd.Connection=this.m_SqlConn; cmd.Transaction=trans; try { foreach(System.String s in sql) { cmd.CommandText=s; cmd.ExecuteNonQuery(); } trans.Commit(); } catch(Exception ex) { trans.Rollback("trans1"); throw new ArgumentException(ex.Message); } finally { this.CloseDatabase(); } } //for sqlclient and oledb public DataSet GetDataSet(System.String sql,System.Boolean isStoredProcedure,params System.Object[] parameters) { DataSet ds=new DataSet(); try { if(this.m_IsSQLServer) { this.m_SqlConn.ConnectionString=this.m_ConnectionString; SqlCommand cmd=new SqlCommand(sql,this.m_SqlConn); foreach(System.Object obj in parameters) { cmd.Parameters.Add((SqlParameter)obj); } if(isStoredProcedure) { cmd.CommandType=CommandType.StoredProcedure; } SqlDataAdapter da=new SqlDataAdapter(cmd); da.Fill(ds); da.Dispose(); cmd.Dispose(); } else { this.m_DbConn.ConnectionString=this.m_ConnectionString; OleDbCommand cmd=new OleDbCommand(sql,this.m_DbConn); foreach(System.Object obj in parameters) { cmd.Parameters.Add((OleDbParameter)obj); } if(isStoredProcedure) { cmd.CommandType=CommandType.StoredProcedure; } OleDbDataAdapter da=new OleDbDataAdapter(cmd); da.Fill(ds); da.Dispose(); cmd.Dispose(); } } catch(Exception ex) { throw new ArgumentException(ex.Message); } return ds; } //for sqlclient and oledb public System.Object ExecuteScalar(System.String sql,System.Boolean isStoredProcedure,params System.Object[] parameters) { System.Object obj=null; try { if(this.m_IsSQLServer) { SqlCommand cmd=new SqlCommand(sql,this.m_SqlConn); foreach(System.Object o in parameters) { cmd.Parameters.Add((SqlParameter)o); } if(isStoredProcedure) { cmd.CommandType=CommandType.StoredProcedure; } this.OpenDatabase(); obj=cmd.ExecuteScalar(); cmd.Dispose(); this.CloseDatabase(); } else { OleDbCommand cmd=new OleDbCommand(sql,this.m_DbConn); foreach(System.Object o in parameters) { cmd.Parameters.Add((OleDbParameter)o); } if(isStoredProcedure) { cmd.CommandType=CommandType.StoredProcedure; } this.OpenDatabase(); obj=cmd.ExecuteScalar(); cmd.Dispose(); this.CloseDatabase(); } } catch(Exception ex) { throw new ArgumentException(ex.Message); } return obj; } } }