using System; using System.Collections.Generic; using System.Text; using System.Data.Common; using System.Data.SqlClient; using System.Data.OracleClient; namespace fnSwordLibrary.Data { public class CuConnection : IDisposable { #region 数据成员 private String _ConnectionString = null; private DbConnection _Connection = null; private CuDbType _DbType = CuDbType.SQL; private String _Description = String.Empty; #endregion #region 构造函数 public CuConnection(CuDbType DbType) { this.DbType = DbType; } public CuConnection(String connection, CuDbType DbType) : this(DbType) { this._ConnectionString = connection; } #endregion #region 属性 public String ConnectionString { get { return this._ConnectionString; } set { this._ConnectionString = value; } } public DbConnection Connection { get { return this._Connection; } } public CuDbType DbType { set { this._DbType = value; this._Connection = this._DbType == CuDbType.SQL ? (DbConnection)new SqlConnection() : new OracleConnection(); } get { return this._DbType; } } public String Description { get { return this._Description; } } #endregion #region 成员方法 public Boolean Open() { try { this._Connection.ConnectionString = this._ConnectionString; this._Connection.Open(); } catch (Exception ex) { _Description = "open database error: " + ex.Message; return false; } return true; } public void Close() { this._Connection.Close(); } public void Dispose() { this._Connection.Dispose(); } #endregion } } // 枚举数据库类型 namespace fnSwordLibrary.Data { public enum CuDbType : int { /// /// ms sql server /// SQL = 0, /// /// oracle /// ORA = 1, /// /// unkonw /// UNK = -1 } }