using HelperDB; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace AIMSExtension { public class GPDBConn { private static string _staticConnectionString; private string _dynamicConnectionString; private ErrorInfo _errorInfo = new ErrorInfo(); private SqlConnection _connection; private SqlTransaction _trans = null; private int _commandTimeout = 180; private SqlCommand _command; private SqlDataAdapter _dataAdapter; public string StaticConnectionString { get { bool flag = !string.IsNullOrEmpty(GPDBConn._staticConnectionString); string staticConnectionString; if (flag) { staticConnectionString = GPDBConn._staticConnectionString; } else { this.InitConn(); staticConnectionString = GPDBConn._staticConnectionString; } return staticConnectionString; } set { GPDBConn._staticConnectionString = value; } } public string ConnectionString { get { bool flag = !string.IsNullOrEmpty(this._dynamicConnectionString); string result; if (flag) { result = this._dynamicConnectionString; } else { bool flag2 = !string.IsNullOrEmpty(GPDBConn._staticConnectionString); if (flag2) { result = GPDBConn._staticConnectionString; } else { this.InitConn(); result = GPDBConn._staticConnectionString; } } return result; } set { this._dynamicConnectionString = value; bool flag = string.IsNullOrEmpty(GPDBConn._staticConnectionString); if (flag) { GPDBConn._staticConnectionString = value; } } } public ErrorInfo Error { get { bool flag = this._errorInfo.Number == 0; if (flag) { this._errorInfo.Message = ""; this._errorInfo.Source = ""; } return this._errorInfo; } } public SqlConnection Connection { get { bool flag = this._connection == null; if (flag) { this._connection = this.GetConnection(""); } return this._connection; } } public SqlTransaction Transaction { get { return this._trans; } set { bool flag = value == null; if (flag) { this._errorInfo.Number = 10007; this._errorInfo.Message = "参数错误。"; this._errorInfo.Source = "Set Transaction Property"; throw new Exception("Set Transaction Property: 参数错误。"); } bool flag2 = this._trans != null; if (flag2) { this._errorInfo.Number = 10002; this._errorInfo.Message = "已经开始事物。"; this._errorInfo.Source = "Set Transaction Property"; throw new Exception("Set Transaction Property:已经开始事物。"); } this._trans = value; } } public int CommandTimeout { get { return this._commandTimeout; } set { this._commandTimeout = value; } } public SqlCommand Command { get { bool flag = this._command == null; if (flag) { this._command = this.Connection.CreateCommand(); this._command.CommandType = CommandType.Text; this._command.CommandTimeout = this._commandTimeout; this.OpenConn(null); } this._command.Transaction = this._trans; return this._command; } set { this._command = value; } } public SqlDataAdapter DataAdapter { get { bool flag = this._dataAdapter == null; if (flag) { this._dataAdapter = new SqlDataAdapter("", this.Connection); } return this._dataAdapter; } set { this._dataAdapter = value; } } public bool IsTransaction { get { return this._trans != null; } } public GPDBConn() { this.InitConn(); } public GPDBConn(string connectString) { bool flag = !string.IsNullOrEmpty(connectString); if (flag) { this.ConnectionString = connectString; } else { this.InitConn(); } } private void InitConn() { bool flag = string.IsNullOrEmpty(GPDBConn._staticConnectionString); if (flag) { GPDBConn._staticConnectionString = new XmlUse(Application.StartupPath + "\\AIMS.xml").GetNode("ConnectionString")[0].ToString(); } } public SqlCommand CreateCommand() { SqlCommand sqlCommand = this.Connection.CreateCommand(); sqlCommand.CommandTimeout = this._commandTimeout; bool flag = this._trans != null; if (flag) { sqlCommand.Transaction = this._trans; } this.OpenConn(null); return sqlCommand; } public SqlConnection GetConnection(string connString = "") { SqlConnection sqlConnection = new SqlConnection(); this._errorInfo.Number = 0; bool flag = string.IsNullOrEmpty(connString); if (flag) { connString = this.ConnectionString; bool flag2 = string.IsNullOrEmpty(connString); if (flag2) { this._errorInfo.Number = 10000; this._errorInfo.Message = "没有设置连接字符串。"; this._errorInfo.Source = "GetConnection"; throw new Exception(this._errorInfo.Message); } } sqlConnection.ConnectionString = connString; return sqlConnection; } public bool OpenConn(SqlConnection conn = null) { this._errorInfo.Number = 0; bool flag = false; bool flag2 = conn == null; if (flag2) { conn = this.Connection; bool flag3 = conn == null; if (flag3) { this._errorInfo.Number = 10001; this._errorInfo.Message = "参数错误。"; this._errorInfo.Source = "OpenConn"; throw new Exception("OpenConn:参数错误。"); } } bool flag4 = conn.State == ConnectionState.Open; bool result; if (flag4) { result = true; } else { try { conn.Open(); bool flag5 = conn.State == ConnectionState.Open; if (flag5) { this._command = this._connection.CreateCommand(); flag = true; } this._command.Transaction = this._trans; } catch (Exception ex) { this._errorInfo.Number = 10020; this._errorInfo.Message = ex.Message; this._errorInfo.Source = "OpenConn"; } result = flag; } return result; } public bool CloseConn() { bool flag = this._connection == null; if (flag) { this._errorInfo.Number = 10005; this._errorInfo.Message = "没有建立连接"; this._errorInfo.Source = "CloseConn"; throw new Exception("CloseConn:没有建立连接。"); } bool flag2 = this._connection.State == ConnectionState.Open; bool result; if (flag2) { try { this._connection.Close(); } catch (Exception ) { //GPLog.Write(string.Format("CloseConn Error: {0}", ex.Message)); } result = (this._connection.State == ConnectionState.Closed); } else { result = true; } return result; } public SqlTransaction BeginTransaction() { this._errorInfo.Number = 0; bool flag = this._trans != null; if (flag) { this._errorInfo.Number = 10002; this._errorInfo.Message = "已经开始事物。"; this._errorInfo.Source = "BeginTransaction"; throw new Exception("BeginTransaction:已经开始事物。"); } bool flag2 = this._connection == null; if (flag2) { this._connection = this.GetConnection(""); } this.OpenConn(null); this._trans = this._connection.BeginTransaction(); this._command.Transaction = this._trans; return this._trans; } public bool Commit() { this._errorInfo.Number = 0; bool flag = this._trans == null; if (flag) { this._errorInfo.Number = 10003; this._errorInfo.Message = "没有开始事物。"; this._errorInfo.Source = "Commit"; throw new Exception("Commit:没有开始事物。"); } this._trans.Commit(); this._trans = null; return true; } public bool RollBack() { this._errorInfo.Number = 0; bool flag = this._trans == null; bool result; if (flag) { this._errorInfo.Number = 10004; this._errorInfo.Message = "没有开始事物。"; this._errorInfo.Source = "RollBack"; result = false; } else { this._trans.Rollback(); this._trans = null; result = true; } return result; } public int Execute(string sql) { this.OpenConn(null); SqlCommand command = this.Command; bool flag = command.Parameters.Count > 0; if (flag) { command.Parameters.Clear(); } this._command.CommandText = sql; return this._command.ExecuteNonQuery(); } public int Execute(string sql, SqlParameter[] parameters) { this.OpenConn(null); SqlCommand command = this.Command; bool flag = command.Parameters.Count > 0; if (flag) { command.Parameters.Clear(); } this._command.CommandText = sql; this._command.Parameters.AddRange(parameters); int result = this._command.ExecuteNonQuery(); this._command.Parameters.Clear(); return result; } public int Execute(List list) { this.OpenConn(null); SqlCommand command = this.Command; bool flag = command.Parameters.Count > 0; if (flag) { command.Parameters.Clear(); } int num = 0; foreach (string current in list) { this._command.CommandText = current; num += this._command.ExecuteNonQuery(); } return num; } public int Execute(string[] sqlArray) { this.OpenConn(null); SqlCommand command = this.Command; bool flag = command.Parameters.Count > 0; if (flag) { command.Parameters.Clear(); } int num = 0; for (int i = 0; i < sqlArray.Length; i++) { string commandText = sqlArray[i]; this._command.CommandText = commandText; num += this._command.ExecuteNonQuery(); } return num; } public object ExecuteScalar(string sql) { this.OpenConn(null); SqlCommand command = this.Command; bool flag = command.Parameters.Count > 0; if (flag) { command.Parameters.Clear(); } command.CommandText = sql; return command.ExecuteScalar(); } } }