private DataCommand DeriveCommand(string commandName) { DataCommand dcmd = null; using (IDbConnection con = _dataSource.CreateConnection()) { con.Open(); IDbCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = commandName; cmd.Connection = con; object commandBuilder = _dataSource.CreateCommandBuilder(); try { _dataSource.Provider.DeriveParametersMethod .Invoke(commandBuilder, new object[] { cmd }); } catch(InvalidOperationException) { //log this return dcmd; } cmd.Connection = null; //fix for InputOutput parameters (DeriveParameters creates always InputOutput //and never Output parameters) //all InputOutput -> Output parameters, therefore //in the sproc definition an OUTPUT param cannot be used also as an INPUT foreach(IDataParameter param in cmd.Parameters) { if (param.Direction == ParameterDirection.InputOutput) param.Direction = ParameterDirection.Output; } dcmd = new DataCommand(cmd, _dataSource); dcmd.Name = commandName; } return dcmd; }
private IDataCommand CreateDataCommand(dataCommand dc) { IDbCommand currentDbCommand = (IDbCommand)Activator.CreateInstance(_provider.CommandObjectType); DataCommand currentDataCommand = new DataCommand(dc.name, currentDbCommand, _dataSource); CommandType commandType = (CommandType)Enum.Parse(typeof(CommandType), dc.type , true); currentDbCommand.CommandType = commandType; currentDbCommand.CommandText = dc.commandText.Trim('\t','\r','\n'); if(dc.replaceByParamValues != null) { string defaultString; foreach(replaceByParamValue rbp in dc.replaceByParamValues) { defaultString = rbp.defaultString; if(defaultString != null) defaultString = defaultString.Trim('\t','\r','\n'); currentDataCommand.AddReplaceByParamValue( rbp.paramName, rbp.paramValue, rbp.oldString.Trim('\t','\r','\n'), rbp.newString.Trim('\t','\r','\n'), defaultString); } } if(dc.parameters != null) { foreach(param p in dc.parameters) { if(p.key == null) p.key = string.Empty; System.Enum paramType = (System.Enum)Enum.Parse(_provider.ParameterDbType, p.type, true); ParameterDirection paramDirection = (ParameterDirection)Enum.Parse( typeof(ParameterDirection), p.direction); DataRowVersion sourceVersion = DataRowVersion.Default; if(p.sourceVersion != null) sourceVersion = (DataRowVersion)Enum.Parse( typeof(DataRowVersion), p.sourceVersion, true); currentDataCommand.Parameters.Add(p.key, p.name, paramType, p.size, paramDirection, p.sourceColumn, sourceVersion, p.isNullable, (byte)p.scale); } } else if(dc.populateParameters) { //TO DO: fix this hack IDataCommand oldCmd = currentDataCommand; currentDataCommand = this.DeriveCommand( currentDataCommand.DbCommand.CommandText); currentDataCommand.Name = oldCmd.Name; } //set timeout if specified at command/datasource level if(dc.timeout != -1) currentDataCommand.DbCommand.CommandTimeout = dc.timeout; else if(_dataSource.CommandTimeout != -1) currentDataCommand.DbCommand.CommandTimeout = _dataSource.CommandTimeout; return currentDataCommand; }
public IDataCommand CreateCommand() { IDbCommand dbCmd = null; if(_templateCommand is ICloneable) { dbCmd = (IDbCommand)((ICloneable)_templateCommand).Clone(); } else { dbCmd = (IDbCommand)Activator.CreateInstance(_provider.CommandObjectType); } IDataCommand cmd = new DataCommand(dbCmd, this); return cmd; }