/// <summary> /// 检索SQL参数信息并填充 /// </summary> /// <param name="cmd"></param> public void DeriveParameters(IDbCommand cmd) { if ((cmd as OracleCommand) != null) { OracleCommandBuilder.DeriveParameters(cmd as OracleCommand); } }
public OracleCommand execSP(string SP, params object[] parametros) { try { OracleCommand cmd = new OracleCommand(SP, conn); cmd.CommandType = CommandType.StoredProcedure; //abrirConexion(); OracleCommandBuilder.DeriveParameters(cmd); int cuenta = 0; foreach (OracleParameter param in cmd.Parameters) { if (param.ParameterName != "RETURN_VALUE") { param.Value = parametros[cuenta]; cuenta++; } } cmd.ExecuteNonQuery(); return(cmd); } catch (Exception ex) { throw new Exception(ex.Message); } }
DbCommand PrepareProcCommand(Database db, string procname, Dictionary <string, object> args) { DbCommand cmd = db.GetStoredProcCommand(procname); using (DbConnection conn = db.CreateConnection()) { try { cmd.Connection = conn; cmd.Connection.Open(); if (db.DBType.Equals(DatabaseType.Oracle)) { OracleCommandBuilder.DeriveParameters((OracleCommand)cmd); } else { SqlCommandBuilder.DeriveParameters((SqlCommand)cmd); } cmd.Connection.Close(); addProcParameters(db, cmd, args); } catch (Exception ex) { throw new ApplicationException("PrepareProcCommand Fail(in Connection Open or DeriveParameters)(Message:" + ex.Message + " procname:" + procname + " db:" + db.DBType.ToString(), ex); } } return(cmd); }
/// <summary> /// Calls the CommandBuilder.DeriveParameters method for the specified provider, doing any setup and cleanup necessary /// </summary> /// <param name="cmd">The IDbCommand referencing the stored procedure from which the parameter information is to be derived. The derived parameters are added to the Parameters collection of the IDbCommand. </param> public override void DeriveParameters(IDbCommand cmd) { bool mustCloseConnection = false; if (!(cmd is OracleCommand)) { throw new ArgumentException("The command provided is not a SqlCommand instance.", "cmd"); } if (cmd.Connection.State != ConnectionState.Open) { cmd.Connection.Open(); mustCloseConnection = true; } OracleCommandBuilder.DeriveParameters((OracleCommand)cmd); if (mustCloseConnection) { cmd.Connection.Close(); } }
public void ExecuteProcedureNonquery(string CommandText, params object[] parameterValues) { OracleConnection connection = null; OracleCommand command = null; string resp = ""; try { connection = CreateConnection(); command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = CommandText; connection.Open(); OracleCommandBuilder.DeriveParameters(command); int index = 0; foreach (OracleParameter parameter in command.Parameters) { if (parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput) { parameter.Value = parameterValues[index]; index++; } else { parameter.Value = new object(); index++; } } command.ExecuteNonQuery(); resp = "success"; } catch (Exception ex) { resp = "error"; //throw; } finally { if (command != null) { command.Dispose(); command = null; } if (connection != null) { if (connection.State == ConnectionState.Open) { connection.Close(); } connection.Dispose(); connection = null; } } }
private void Form1_Load(object sender, EventArgs e) //this is the code which will run when the form loads { con.Open(); OracleCommand oc = new OracleCommand("threetwo.load_ui2", con); oc.CommandType = CommandType.StoredProcedure; OracleCommandBuilder.DeriveParameters(oc); foreach (OracleParameter p in oc.Parameters) { System.Windows.Forms.Label temp = new System.Windows.Forms.Label(); this.Controls.Add(temp); temp.Top = a * 30; temp.Left = 10; temp.AutoSize = true; temp.Text += p.ParameterName.ToString(); temp.Name = "lab" + a.ToString(); ++a; //for textbox System.Windows.Forms.TextBox temp2 = new System.Windows.Forms.TextBox(); this.Controls.Add(temp2); temp2.Top = b * 30; temp2.Left = 100; temp2.Name = "txt" + b.ToString(); ++b; } con.Close(); }
/// <summary> /// <see cref="OracleCommandBuilder.DeriveParameters"/> /// </summary> /// <param name="cmd"></param> public override void DeriveParameters(System.Data.IDbCommand cmd) { if (cmd is OracleCommand) { OracleCommandBuilder.DeriveParameters(cmd as OracleCommand); } }
public IEnumerable <User> GetByIds3(string[] userIds) { using (var connection = new OracleConnection(connectionString)) using (var command = new OracleCommand("USER_PACKAGE.GET_BY_IDS3", connection)) { connection.Open(); command.CommandType = CommandType.StoredProcedure; OracleCommandBuilder.DeriveParameters(command); command.Parameters["P_USER_IDS"].Value = userIds; using (var reader = command.ExecuteReader()) { var output = new List <User>(); if (!reader.HasRows) { return(output); } while (reader.Read()) { var record = new User(); record.UserId = (string)reader["USER_ID"]; record.FirstName = (string)reader["FIRST_NAME"]; record.LastName = (string)reader["LAST_NAME"]; output.Add(record); } return(output); } } }
/// <summary> /// The discover sp parameter set. /// </summary> /// <param name="connectionString"> /// The connection string. /// </param> /// <param name="spName"> /// The sp name. /// </param> /// <param name="includeReturnValueParameter"> /// The include return value parameter. /// </param> /// <returns> /// The <see> /// <cref>OracleParameter[]</cref> /// </see> /// . /// </returns> private static OracleParameter[] DiscoverSpParameterSet( string connectionString, string spName, bool includeReturnValueParameter) { using (var cn = new OracleConnection(connectionString)) using (var cmd = new OracleCommand(spName, cn)) { cn.Open(); cmd.CommandType = CommandType.StoredProcedure; OracleCommandBuilder.DeriveParameters(cmd); if (!includeReturnValueParameter) { if (ParameterDirection.ReturnValue == cmd.Parameters[0].Direction) { cmd.Parameters.RemoveAt(0); } } var discoveredParameters = new OracleParameter[cmd.Parameters.Count]; cmd.Parameters.CopyTo(discoveredParameters, 0); return(discoveredParameters); } }
protected override void DeriveParameters(IDbCommand cmd) { if (!(cmd is OracleCommand)) { throw new ArgumentException("The command provided is not a OleDbCommand instance.", "cmd"); } OracleCommandBuilder.DeriveParameters((OracleCommand)cmd); }
///// <summary> ///// 是否要强制关闭连接 ///// </summary> //private static bool mustCloseConnection = false; /// <summary> /// 返回一个存储过程的参数数组 /// </summary> /// <param name="connection">有效的数据连接</param> /// <param name="spName">存储过程名</param> /// <param name="includeReturnValueParameter">是否包含返回值参数</param> /// <returns>返回找到的参数数组.</returns> private static OracleParameter[] DiscoverSpParameterSet(OracleConnection connection, string spName, bool includeReturnValueParameter) { // 检验参数 if (connection == null) { throw new ArgumentNullException("connection"); } if (spName == null || spName.Length == 0) { throw new ArgumentNullException("spName"); } // 定义一个模拟命令 OracleCommand cmd = new OracleCommand(spName, connection); cmd.CommandType = CommandType.StoredProcedure; // 打开连接 connection.Open(); // 查找存储过程的参数 OracleCommandBuilder.DeriveParameters(cmd); //// 是否要关闭连接 //if (mustCloseConnection == true) //{ // connection.Close(); //} connection.Close(); // 如果包含返回参数,则删除第一个参数,该情况存在于SQL SERVER中 if (!includeReturnValueParameter) { cmd.Parameters.RemoveAt(0); } // 返回找到的参数的副本 OracleParameter[] discoveredParameters = new OracleParameter[cmd.Parameters.Count]; cmd.Parameters.CopyTo(discoveredParameters, 0); // 参数一刀两断,没有关系了. cmd.Parameters.Clear(); // 初始化各参数为DBNull foreach (OracleParameter discoveredParameter in discoveredParameters) { discoveredParameter.Value = DBNull.Value; } return(discoveredParameters); }
/// <summary> /// Retrieves parameter information from the stored procedure specified in the <see cref="DbCommand"/> and populates the Parameters collection of the specified <see cref="DbCommand"/> object. /// </summary> /// <param name="discoveryCommand">The <see cref="DbCommand"/> to do the discovery.</param> /// <remarks> /// The <see cref="DbCommand"/> must be an instance of a <see cref="OracleCommand"/> object. /// </remarks> protected override void DeriveParameters(DbCommand discoveryCommand) { OracleCommandBuilder.DeriveParameters((OracleCommand)discoveryCommand); OracleParameter curOut = discoveryCommand.Parameters[RefCursorName] as OracleParameter; if (curOut != null) { curOut.OracleDbType = OracleDbType.RefCursor; } }
internal OracleCommand ObtenerComando(string nombreprocedimiento) { OracleCommand command = this.fwconexion.CreateCommand(); command.CommandText = nombreprocedimiento; command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = this.timeout; OracleCommandBuilder.DeriveParameters(command); return(command); }
private void AttachParameters(DbCommand command, Hashtable commandParameters) { //If its a oracle procedure/package if (command is OracleCommand && command.CommandType == CommandType.StoredProcedure) { if (_connection.State != ConnectionState.Open) { _connection.Open(); } //Derive parameter types, directions and sizes from Oracle DB OracleCommandBuilder.DeriveParameters((OracleCommand)command); if (command.Parameters.Count != commandParameters.Count) { throw new ArgumentException(Resources.MsgParameterNumberDiffers); } foreach (var k in commandParameters.Keys) { var key = k.ToString().ToUpper(); if (!command.Parameters.Contains(key)) { throw new IndexOutOfRangeException(string.Format(Resources.MsgParameterNotFound, key)); } command.Parameters[key].Value = (commandParameters[key] ?? DBNull.Value); } } else { foreach (var k in commandParameters.Keys) { var key = k.ToString().ToUpper(); //Check for null values and replaces by DBNull var value = (commandParameters[key] ?? DBNull.Value); if (command is OracleCommand) { command.Parameters.Add(new OracleParameter(key, value)); } else if (command is SqlCommand) { command.Parameters.Add(new SqlParameter(key, value)); } else if (command is OleDbCommand) { command.Parameters.Add(new OleDbParameter(key, value)); } } } }
/// <summary> /// Start the DB Process /// </summary> /// <param name="procedureName">name of the process</param> /// <returns></returns> /// #pragma warning disable 612, 618 private bool StartDBProcess(String procedureName) { if (DatabaseType.IsPostgre) //jz Only DB2 not support stored procedure now { return(false); } // execute on this thread/connection //String sql = "{call " + procedureName + "(" + _pi.GetAD_PInstance_ID() + ")}"; OracleConnection conn = null; try { //only oracle procedure are supported OracleCommand comm = new OracleCommand(); conn = (OracleConnection)DataBase.DB.GetConnection(); conn.Open(); comm.Connection = conn; comm.CommandText = procedureName; comm.CommandType = CommandType.StoredProcedure; OracleCommandBuilder.DeriveParameters(comm); OracleParameter[] param = new OracleParameter[1]; foreach (OracleParameter orp in comm.Parameters) { param[0] = new OracleParameter(orp.ParameterName, _pi.GetAD_PInstance_ID()); } //log.Fine("Executing " + procedureName + "(" + _pi.GetAD_PInstance_ID() + ")"); int res = SqlExec.Oracle.OracleHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, procedureName, param); conn.Close(); if (res < 0) { ProcessInfoUtil.SetParameterFromDB(_pi); return(StartDBProcess(procedureName, _pi.GetParameter())); } //DataBase.DB.ExecuteQuery(sql, null); } catch (Exception e) { if (conn != null) { conn.Close(); } //log.Log(Level.SEVERE, "Error executing procedure " + procedureName, e); _pi.SetSummary(Msg.GetMsg(_ctx, "ProcessRunError") + " " + e.Message); _pi.SetError(true); return(false); } // log.fine(Log.l4_Data, "ProcessCtl.startProcess - done"); return(true); }
/// <summary> /// Resolve at run time the appropriate set of DbParameters for a stored procedure /// </summary> /// <param name="connection">A valid DbConnection object</param> /// <param name="spName">The name of the stored procedure</param> /// <param name="includeReturnValueParameter">Whether or not to include their return value parameter</param> /// <returns>The parameter array discovered.</returns> private static DbParameter[] DiscoverSpParameterSet(DbConnection connection, string spName, bool includeReturnValueParameter) { if (connection == null) { throw new ArgumentNullException("connection"); } if (spName == null || spName.Length == 0) { throw new ArgumentNullException("spName"); } DbCommand cmd = connection.CreateCommand(); cmd.CommandText = spName; cmd.CommandType = CommandType.StoredProcedure; connection.Open(); if (connection is SqlConnection) { SqlCommandBuilder.DeriveParameters((SqlCommand)cmd); } else if (connection is OracleConnection) { OracleCommandBuilder.DeriveParameters((OracleCommand)cmd); } else if (connection is OdbcConnection) { OdbcCommandBuilder.DeriveParameters((OdbcCommand)cmd); } else if (connection is OleDbConnection) { OleDbCommandBuilder.DeriveParameters((OleDbCommand)cmd); } connection.Close(); if (!includeReturnValueParameter) { cmd.Parameters.RemoveAt(0); } DbParameter[] discoveredParameters = new DbParameter[cmd.Parameters.Count]; cmd.Parameters.CopyTo(discoveredParameters, 0); // Init the parameters with a DBNull value foreach (DbParameter discoveredParameter in discoveredParameters) { discoveredParameter.Value = DBNull.Value; } return(discoveredParameters); }
public override Params ExecuteStoredProcedure(string strProcedureName, params object[] cmdParams) { Params params2; Params @params = new OracleParams(); OracleConnection objConnection = (OracleConnection)base.objConnection; try { OracleCommand command = new OracleCommand(strProcedureName, objConnection) { CommandType = CommandType.StoredProcedure }; if (objConnection.State != ConnectionState.Open) { objConnection.Open(); } OracleCommandBuilder.DeriveParameters(command); for (int i = 0; i < command.Parameters.Count; i++) { if (command.Parameters[i].Direction == ParameterDirection.Input) { if (cmdParams.Length <= i) { throw new Exception("Parameter Count does not match."); } command.Parameters[i].Value = cmdParams[i]; } else { command.Parameters[i].Value = DBNull.Value; } } command.ExecuteNonQuery(); foreach (OracleParameter parameter in command.Parameters) { if ((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Output)) { @params.Add(parameter); } } params2 = @params; } catch (Exception exception) { throw exception; } finally { this.CloseConnection(); } return(params2); }
void IDatabase.DeriveParameters() { try { OracleCommandBuilder.DeriveParameters(this.sqlDataAdapter.SelectCommand); } catch (AtomusException exception) { throw exception; } catch (Exception exception) { throw new AtomusException(exception); } }
public static void ListParms() { OracleConnection conn = new OracleConnection("SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.3.1.11)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=MDMSPMIS)));User Id=MDMS;Password=MDMS;"); OracleCommand cmd = new OracleCommand("PKG_WEBSERVICE_MDMS.Get_DiemDo", conn); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); OracleCommandBuilder.DeriveParameters(cmd); foreach (OracleParameter p in cmd.Parameters) { string acb = p.ParameterName + " " + p.OracleType + " " + p.Value + " " + p.Size; var dir = p.Direction; var dirf = p.Offset; } }
/// <summary> /// Derives the parameters of the given DbCommand object /// </summary> /// <param name="dbCmd">A DAAB DbCommand object</param> public override void DeriveParameters(DbCommand dbCmd) { OracleCommand oracleCmd = (OracleCommand)dbCmd; oracleCmd.Connection = (OracleConnection)_database.CreateConnection(); try { oracleCmd.Connection.Open(); OracleCommandBuilder.DeriveParameters(oracleCmd); } finally { oracleCmd.Connection.Close(); oracleCmd.Connection.Dispose(); } }
static partial void OracleDeriveParameters(DbCommand dbCmd, ref bool processed) { if (processed) { return; } OracleCommand oraCmd = dbCmd as OracleCommand; if (oraCmd != null) { ShuffleCommandTextCase(oraCmd); OracleCommandBuilder.DeriveParameters(oraCmd); processed = true; } }
/// <summary> /// <para>Dicover the parameters for a stored procedure using a separate connection and command.</para> /// </summary> /// <param name="parameterToken"><para>The parameter delimeter for database commands.</para></param> protected override void DoDiscoverParameters(char parameterToken) { this.parameterToken = parameterToken; using (OracleCommand newCommand = CreateNewCommandAndConnectionForDiscovery()) { OracleCommandBuilder.DeriveParameters(newCommand); foreach (IDataParameter parameter in newCommand.Parameters) { IDataParameter cloneParameter = (IDataParameter)((ICloneable)parameter).Clone(); cloneParameter.ParameterName = cloneParameter.ParameterName; this.command.Parameters.Add(cloneParameter); } newCommand.Connection.Close(); } }
/// <summary> /// resolve at run time the appropriate set of OracleParameters for a stored procedure /// </summary> /// <param name="connectionString">a valid connection string for a OracleConnection</param> /// <param name="spName">the name of the stored procedure</param> /// <param name="includeReturnValueParameter">whether or not to include their return value parameter</param> /// <returns></returns> private static OracleParameter[] DiscoverSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter) { using (OracleConnection cn = new OracleConnection(connectionString)) using (OracleCommand cmd = new OracleCommand(spName, cn)) { cn.Open(); cmd.CommandType = CommandType.StoredProcedure; OracleCommandBuilder.DeriveParameters(cmd); OracleParameter[] discoveredParameters = new OracleParameter[cmd.Parameters.Count];; cmd.Parameters.CopyTo(discoveredParameters, 0); return(discoveredParameters); } }
private void DeriveParameters(DbCommand discoveryCommand) { if (discoveryCommand.CommandType != CommandType.StoredProcedure) { return; } if (this._dbProviderFactory is SqlClientFactory) { SqlCommandBuilder.DeriveParameters((SqlCommand)discoveryCommand); } if (this._dbProviderFactory is OracleClientFactory) { OracleCommandBuilder.DeriveParameters((OracleCommand)discoveryCommand); } }
public void Update_CommandBuilder_DeriveParameters(User record) { using (var connection = new OracleConnection(connectionString)) using (var command = new OracleCommand("USER_PACKAGE.UPD", connection)) { connection.Open(); command.CommandType = CommandType.StoredProcedure; // Command Builder can also generate Insert, Update, and Delete commands when given a data adapter. // See https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/generating-commands-with-commandbuilders?view=netframework-4.7.2 OracleCommandBuilder.DeriveParameters(command); command.Parameters["P_USER_ID"].Value = record.UserId; command.Parameters["P_FIRST_NAME"].Value = record.FirstName; command.Parameters["P_LAST_NAME"].Value = record.LastName; var rowsAffected = command.ExecuteNonQuery(); // rowsAffected returns -1 even when successful, might look into why this is happening } }
public override DataSet ExecuteDataSet(string strCommandText, CommandType cmdType, params object[] cmdParams) { OracleConnection objConnection = (OracleConnection)base.objConnection; OracleCommand command = new OracleCommand(strCommandText, objConnection); List <int> list = new List <int>(); command.CommandType = cmdType; if (cmdType == CommandType.StoredProcedure) { try { if (objConnection.State != ConnectionState.Open) { objConnection.Open(); } OracleCommandBuilder.DeriveParameters(command); objConnection.Close(); for (int j = 0; j < command.Parameters.Count; j++) { OracleParameter parameter = command.Parameters[j]; if (parameter.OracleType != OracleType.Cursor) { list.Add(j); } } } catch (OracleException exception) { throw exception; } for (int i = 0; i < list.Count; i++) { if (cmdParams.Length <= i) { throw new Exception("Parameter Count does not match."); } command.Parameters[list[i]].Value = cmdParams[i]; } } OracleDataAdapter adapter = new OracleDataAdapter(command); DataSet dataSet = new DataSet(); adapter.Fill(dataSet); return(dataSet); }
/// <summary> /// Recupera información de parámetro del procedimiento almacenado especificado en SqlCommand /// y rellena la colección de Parameters del objeto SqlCommand especificado. /// </summary> /// <param name="server">Servidor a utilizar</param> /// <param name="command">Commando a rellenar con parametros</param> public static void DeriveParameter(ServerEnumType server, DbCommand command) { switch (server) { case ServerEnumType.SqlServer: SqlCommandBuilder.DeriveParameters(command as SqlCommand); command.Parameters.RemoveAt("@RETURN_VALUE"); break; case ServerEnumType.MySQL: MySqlCommandBuilder.DeriveParameters(command as MySqlCommand); break; case ServerEnumType.Oracle: OracleCommandBuilder.DeriveParameters(command as OracleCommand); break; } }
/// <summary> /// <para>Dicover the parameters for a stored procedure using a separate connection and command.</para> /// </summary> /// <param name="parameterToken"><para>The parameter delimeter for database commands.</para></param> protected override void DoDiscoverParameters(char parameterToken) { this.parameterToken = parameterToken; using (OracleCommand newCommand = CreateNewCommandAndConnectionForDiscovery()) { // Must use ODP.Net 10.1.0.3.01 to get this method see http://www.oracle.com/technology/docs/tech/windows/odpnet/readme10103.txt. OracleCommandBuilder.DeriveParameters(newCommand); foreach (IDataParameter parameter in newCommand.Parameters) { IDataParameter cloneParameter = (IDataParameter)((ICloneable)parameter).Clone(); cloneParameter.ParameterName = cloneParameter.ParameterName; this.command.Parameters.Add(cloneParameter); } newCommand.Connection.Close(); } }
public void DeriveParameters(IDbCommand command) { var oracleCommand = (OracleCommand)command; OracleCommandBuilder.DeriveParameters(oracleCommand); //OracleCommand oracleCommand = (OracleCommand)command; //oracleCommand.BindByName = true; //string commandText = oracleCommand.CommandText; //string[] items = commandText.Split('.'); //string owner = items[0]; //string packageName = items[1]; //string objectName = items[2]; //CommandBuilder.DeriveParameters(oracleCommand.Connection, owner, packageName, objectName, "1", oracleCommand.Parameters); //if (command.Parameters.Count == 0) // CommandBuilder.DeriveParameters(oracleCommand.Connection, owner, packageName, objectName, null, oracleCommand.Parameters); }
public DataTable SPRefcursor(string SP, params object[] parametros) { try { DataTable dt = new DataTable(); connection = new OracleConnection(oradb); command = new OracleCommand(SP, connection); command.CommandType = CommandType.StoredProcedure; connection.Open(); OracleCommandBuilder.DeriveParameters(command); int cuenta = 0; foreach (OracleParameter pr in command.Parameters) { switch (pr.ParameterName) { case "P_REFCURSOR": pr.Direction = ParameterDirection.Output; OracleDataAdapter oda = new OracleDataAdapter(command); command.ExecuteNonQuery(); oda.Fill(dt); break; default: pr.Value = parametros[cuenta]; cuenta++; break; } } connection.Close(); command.Dispose(); return(dt); } catch (Exception ex) { return(null); throw; } }