protected override CustomerCommand CreateCommand(DataCommandConfig commandConfig) { //1.创建 Connection 对象【需要从连接池中获取连接对象,此处后续优化】 //string strConn = ConfigurationManager.AppSettings["ConnectionString"]; var dbConfig = DataManager.DatabaseDictionary[commandConfig.DataSourceID]; string strConn = dbConfig.ConnectionString; strConn = string.Format(strConn, ConfigurationManager.AppSettings["Tests.Environment"].ToLower()); SqlConnection dbConnection = new SqlConnection(strConn); //2.创建 Command 对象 SqlCommand command = new SqlCommand(commandConfig.CommandText, dbConnection); command.CommandType = CommandType.Text; //3.填充参数列表 foreach (ParameterConfig param in commandConfig.Parameters) { SqlParameter parameter = command.CreateParameter(); parameter.ParameterName = param.Name; parameter.DbType = param.DBType; parameter.Size = param.Size == 0 ? 4 : param.Size; parameter.Direction = ParameterDirection.Input; //默认是输入参数 command.Parameters.Add(parameter); } CustomerCommand customerCmd = new CustomerCommand(command, DataBaseType.SQLServer); return(customerCmd); }
/// <summary> /// 创建可用的Command对象 /// </summary> /// <param name="commandName">需要获取的Command名称</param> /// <returns></returns> public CustomerCommand CreateCustomerCommand(string commandName) { /* * 1.获取 XML 数据 * 2.获取对应的 Commmand 信息 * 3.填充 Command 对象并返回数据 */ //#region 1.获取 XML 数据 ////1.获取 XML 数据【可修改问配置文件】 //string commandFilePath = ConfigurationManager.AppSettings["CommandFilePath"]; //string strNamespace = @"https:\\FellowshipOne\Framework\DataOperators"; //FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + commandFilePath); //XmlSerializer xmls = new XmlSerializer(typeof(DataOperatorsConfig), strNamespace); //DataOperatorsConfig DataOperators = xmls.Deserialize(fs) as DataOperatorsConfig; //fs.Close(); //#endregion #region 2.获取对应的 Commmand 信息 //2.获取对应的 Commmand 信息 //DataCommandConfig commandConfig = DataOperators.DataCommands.Find(x => x.Name == commandName); if (!DataManager.CommandDictionary.ContainsKey(commandName)) { throw new Exception(string.Format("未找到Name='{0}'的配置节点,请检查配置文件是否正确。", commandName)); } DataCommandConfig commandConfig = DataManager.CommandDictionary[commandName]; /*后续添加针对 commandConfig 对象的数据校验*/ #endregion //3.填充 Command 对象 CustomerCommand customerCmd = CreateCommand(commandConfig); return(customerCmd); }