/// <summary> /// 获取所有导出部件 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List <T> GetPlugins <T>() { IEnumerable <T> temp = null; try { if (container != null) { if (string.IsNullOrEmpty(PluginName)) { temp = container.GetExportedValues <T>(); if (temp == null || temp.Count() == 0) { throw new Exception("类型为:" + typeof(T).Name + "没有找到导出对象"); } } else { temp = container.GetExportedValues <T>(PluginName); if (temp == null || temp.Count() == 0) { throw new Exception("类型为:" + typeof(T).Name + "没有找到指定协定名称为" + PluginName + "的导出对象"); } } } if (temp != null && temp.Count() > 0) { return(temp.ToList()); } else { throw new Exception("获取插件:" + typeof(T).Name + "失败"); } } catch (Exception ex) { CYFLog.WriteLog(ex.Message); CYFLog.WriteLog("尝试获取默认部件"); try { temp = container.GetExportedValues <T>(); if (temp == null || temp.Count() == 0) { throw new Exception("尝试获取默认部件失败"); } else { return(temp.ToList()); } } catch (Exception ex2) { throw ex2; } } }
public CYFSqlDALHelper(string sqlConnectionStr) { try { conn = new SqlConnection(sqlConnectionStr); connectionStr = conn.ConnectionString; } catch (Exception ex) { CYFLog.WriteLog(CYFLog.LogFile.Exception, ex.Message); } }
/// <summary> /// 执行命令返回DataSet /// </summary> /// <param name="cmd"></param> /// <returns></returns> private DataSet ExecuteSql(SqlCommand cmd) { try { DataSet ds = new DataSet(); //记录数据操作语句 //new Logger.clsLogText().Log2File(string.Format("{0} {1} \r\n{2}", DateTime.Now.ToString(), "ExecuteSql", cmd.CommandText)); if (this.Trans != null) { if (this.Trans.Connection == null) { throw new InvalidOperationException("事务已终止,已无法再使用"); } if (this.Connection.State == ConnectionState.Closed) { this.Connection.Open(); } cmd.Connection = this.Connection; cmd.Transaction = this.Trans; cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); da.Fill(ds); } else { using (SqlConnection sqlcon = new SqlConnection(this.ConnectionString)) { if (sqlcon.State == ConnectionState.Closed) { sqlcon.Open(); } cmd.Connection = sqlcon; cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); da.Fill(ds); } } return(ds); } catch { throw; } }
/// <summary> /// 执行命令返回DataTable /// </summary> /// <param name="cmd"></param> /// <returns></returns> public System.Data.DataTable GetTable(System.Data.SqlClient.SqlCommand cmd) { try { //记录数据操作语句 //new Logger.clsLogText().Log2File(string.Format("{0} {1} \r\n{2}", DateTime.Now.ToString(), "GetTable", cmd.CommandText)); DataTable dt = new DataTable(); if (this.Trans != null) { if (this.Trans.Connection == null) { throw new InvalidOperationException("事务已终止,已无法再使用"); } if (this.Connection.State == ConnectionState.Closed) { this.Connection.Open(); } cmd.Connection = this.Connection; cmd.Transaction = this.Trans; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); da.Fill(dt); } else { using (SqlConnection sqlcon = new SqlConnection(this.ConnectionString)) { if (sqlcon.State == ConnectionState.Closed) { sqlcon.Open(); } cmd.Connection = sqlcon; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); da.Fill(dt); } } return(dt); } catch { throw; } }
/// <summary> /// 执行命令返回DataTable,可以通过Parameters添加参数 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public DataTable ExecuteGetDataTable(SqlCommand cmd) { DataTable dt = new DataTable(); try { if (this.trans == null) { using (SqlConnection connection = new SqlConnection(this.connectionStr)) { if (connection.State == ConnectionState.Closed) { connection.Open(); } cmd.Connection = connection; AddParameters(cmd); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); return(dt); } } else { if (this.trans.Connection == null) { throw new Exception("连接已经关闭,无法执行SQL"); } if (this.conn.State == ConnectionState.Closed) { this.conn.Open(); } cmd.Connection = this.conn; cmd.Transaction = this.trans; AddParameters(cmd); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); return(dt); } } catch (Exception ex) { CYFLog.WriteLog(ex.Message); throw ex; } }
private CYFDALConfig() { string tempStr = string.Empty; if (ConfigurationManager.AppSettings.AllKeys.Contains("ConnectionString")) { tempStr = ConfigurationManager.AppSettings["ConnectionString"]; } if (string.IsNullOrEmpty(tempStr)) { CYFLog.WriteLog("没有在配置中初始化ConnectionString"); } else { _connectionStr = tempStr; } }
/// <summary> /// 执行命令返回第一行第一个值 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public object ExecuteScalar(SqlCommand cmd) { try { //记录数据操作语句 //new Logger.clsLogText().Log2File(string.Format("{0} {1} \r\n{2}", DateTime.Now.ToString(), "ExecuteScalar", cmd.CommandText)); object obj = null; if (this.Trans != null) { if (this.Trans.Connection == null) { throw new InvalidOperationException("事务已终止,已无法再使用"); } if (this.Connection.State == ConnectionState.Closed) { this.Connection.Open(); } cmd.Connection = this.Connection; cmd.Transaction = this.Trans; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); obj = cmd.ExecuteScalar(); } else { using (SqlConnection sqlcon = new SqlConnection(this.ConnectionString)) { if (sqlcon.State == ConnectionState.Closed) { sqlcon.Open(); } cmd.Connection = sqlcon; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); obj = cmd.ExecuteScalar(); } } return(obj); } catch { throw; } }
/// <summary> /// 执行命令,返回影响行数 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public int ExecuteNonQuery(SqlCommand cmd)//执行时共享同一个事务 { try { //记录数据操作语句 int result; if (this.Trans != null) { if (this.Trans.Connection == null) { throw new InvalidOperationException("事务已终止,已无法再使用"); } if (this.Connection.State == ConnectionState.Closed) { this.Connection.Open(); } cmd.Connection = this.Connection; cmd.Transaction = this.Trans; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); result = cmd.ExecuteNonQuery(); } else { using (SqlConnection sqlcon = new SqlConnection(this.ConnectionString)) { if (sqlcon.State == ConnectionState.Closed) { sqlcon.Open(); } cmd.Connection = sqlcon; CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); result = cmd.ExecuteNonQuery(); } } return(result); } catch { throw; } }
/// <summary> /// 执行命令返回影响行数 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public int ExecuteNonQuery(SqlCommand cmd) { try { if (this.trans == null) { using (SqlConnection connection = new SqlConnection(this.connectionStr)) { if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } cmd.Connection = connection; AddParameters(cmd); CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); return(cmd.ExecuteNonQuery()); } } else { if (this.trans.Connection == null) { throw new Exception("连接已经关闭"); } if (this.trans.Connection.State == System.Data.ConnectionState.Closed) { this.trans.Connection.Open(); } cmd.Connection = this.trans.Connection; cmd.Transaction = this.trans; AddParameters(cmd); CYFLog.WriteLog(CYFLog.LogFile.SQL, cmd.CommandText); return(cmd.ExecuteNonQuery()); } } catch (Exception ex) { throw ex; } }