自己寫的資料庫連結類,請高手指點一下。
阿新 • • 發佈:2019-01-25
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; namespace DatabaseConn { public class Connection { private string connection_string; /// <summary> /// 資料庫連結字串 /// </summary> public string ConnectionString { get { return connection_string; } set { connection_string = value; } } private OleDbConnection connection; /// <summary> /// 資料庫連結 /// </summary> public OleDbConnection Conn { get { return connection; } set { connection = value; } } private OleDbCommand command; /// <summary> /// 資料庫命令執行者 /// </summary> public OleDbCommand Command { get { return command; } set { command = value; } } private OleDbDataAdapter dateadapter; private OleDbDataReader datereader; /// <summary> /// 資料庫讀取器 /// </summary> public OleDbDataReader DateReader { get { return datereader; } set { datereader = value; } } private string sql; /// <summary> /// 執行SQL /// </summary> public string SQL { get { return sql; } set { sql = value; } } private string error_info; /// <summary> /// 錯誤資訊 /// </summary> public string ErrorInfo { get { return error_info; } set { error_info = value; } } /// <summary> /// 初始化連結 /// </summary> public Connection() { connection = new OleDbConnection(); command = new OleDbCommand(); command.Connection = connection; error_info = ""; sql = ""; connection_string = ""; } /// <summary> /// 初始化連結 /// </summary> /// <param name="ConnectionString">資料庫連結字串</param> public Connection(string ConnectionString) { connection = new OleDbConnection(ConnectionString); command = new OleDbCommand(); command.Connection = connection; error_info = ""; sql = ""; connection_string = ConnectionString; } /// <summary> /// 取得DataTable /// </summary> /// <returns>返回DataTable</returns> public DataTable GetDataTable() { DataTable temp = new DataTable(); try { if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.Text; command.CommandText = sql; dateadapter = new OleDbDataAdapter(); dateadapter.SelectCommand = command; dateadapter.Fill(temp); //清理OleDbCommand.Parameters變數。 if (command.Parameters.Count > 0) { command.Parameters.Clear(); } } catch (Exception e) { Dispose(); error_info = e.Message; throw e; } return temp; } /// <summary> /// 取得DataSet /// </summary> /// <returns>返回DataSet</returns> public DataSet GetDataSet() { DataSet temp = new DataSet(); try { if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.Text; command.CommandText = sql; dateadapter = new OleDbDataAdapter(); dateadapter.SelectCommand = command; dateadapter.Fill(temp); //清理OleDbCommand.Parameters變數。 if (command.Parameters.Count > 0) { command.Parameters.Clear(); } } catch (Exception e) { Dispose(); error_info = e.Message; throw e; } return temp; } /// <summary> /// 執行SQL命令 /// </summary> /// <returns>返回影響行數</returns> public int ExecuteSQL() { int temp = 0; try { if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.Text; command.CommandText = sql; command.Connection = connection; temp = command.ExecuteNonQuery(); //清理OleDbCommand.Parameters變數。 if (command.Parameters.Count > 0) { command.Parameters.Clear(); } } catch (Exception e) { Dispose(); error_info = e.Message; throw e; } return temp; } /// <summary> /// 取得第一列第一行 /// </summary> /// <returns>返回第一列第一行</returns> public object ExecuteScalar() { object temp = null; try { if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.Text; command.CommandText = sql; command.Connection = connection; temp = command.ExecuteScalar(); //清理OleDbCommand.Parameters變數。 if (command.Parameters.Count > 0) { command.Parameters.Clear(); } } catch (Exception e) { Dispose(); error_info = e.Message; throw e; } return temp; } /// <summary> /// 取得讀取器 /// </summary> /// <returns>返回讀取器</returns> public object GetDataReader() { object temp = null; try { if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.Text; command.CommandText = sql; command.Connection = connection; temp = command.ExecuteReader(); //清理OleDbCommand.Parameters變數。 if (command.Parameters.Count > 0) { command.Parameters.Clear(); } } catch (Exception e) { Dispose(); error_info = e.Message; throw e; } return temp; } /// <summary> /// 關閉釋放所有資源連結 /// </summary> public void Dispose() { if (dateadapter != null) { dateadapter.Dispose(); } if (datereader != null) { if (datereader.IsClosed == true) { datereader.Close(); } datereader.Dispose(); } if (connection != null) { if (connection.State != ConnectionState.Closed) { connection.Close(); } connection.Dispose(); } if (command != null) { command.Dispose(); } } } }