/// <summary> /// /// </summary> /// <param name="cmd"></param> protected virtual void OnDeriveParameters(DbCommand cmd) { DbProviderFactory factory = DbProviderFactories.GetFactory(this.Provider); DbCommandBuilder cb = factory.CreateCommandBuilder(); MethodInfo method = cb.GetType().GetMethod("DeriveParameters", BindingFlags.Static | BindingFlags.Public); method.Invoke(null, new object[] { cmd }); }
private void InitCommandBuilder() { if (_commandBuilder == null) { _commandBuilder = DataSource.CreateCommandBuilder(); _getParameterName = _commandBuilder.GetType().GetMethod("GetParameterName", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(string) }, null); } }
/// <summary> /// 更新系SQLの自動生成を行う /// </summary> /// <param name="factory">データ取得時に指定されたデータプロバイダのDbProviderFactory</param> /// <param name="adapter">データ取得時に生成したDbDataAdapter</param> /// <param name="log">実行ログ出力先のStringBuilder</param> private static void CreateUpdateCommand(DbProviderFactory factory, DbDataAdapter adapter, StringBuilder log) { // CommandBuilderを生成し、DbDataAdapterに関連付ける DbCommandBuilder cb = factory.CreateCommandBuilder(); cb.DataAdapter = adapter; log.AppendFormat("< {0} クラスのオブジェクトによって動的に生成された SQL >", cb.GetType().ToString()); log.AppendLine(); // INSERT文を生成する log.AppendLine("INSERT SQL コマンド:"); log.AppendLine(cb.GetInsertCommand().CommandText); log.AppendLine(); log.AppendLine("INSERT SQL コマンド パラメータ:"); foreach (DbParameter param in cb.GetInsertCommand().Parameters) { log.AppendFormat("{0} => DbType = {1}, SourceColumn = {2}", param.ParameterName, param.DbType, param.SourceColumn); log.AppendLine(); } log.AppendLine().AppendLine(); // UPDATE文を生成する log.AppendLine("UPDATE SQL コマンド:"); log.AppendLine(cb.GetUpdateCommand().CommandText); log.AppendLine(); log.AppendLine("UPDATE SQL コマンド パラメータ:"); foreach (DbParameter param in cb.GetUpdateCommand().Parameters) { log.AppendFormat("{0} => DbType = {1}, SourceColumn = {2}", param.ParameterName, param.DbType, param.SourceColumn); log.AppendLine(); } log.AppendLine().AppendLine(); // DELETE文を生成する log.AppendLine("DELETE SQL コマンド:"); log.AppendLine(cb.GetDeleteCommand().CommandText); log.AppendLine(); log.AppendLine("DELETE SQL コマンド パラメータ:"); foreach (DbParameter param in cb.GetDeleteCommand().Parameters) { log.AppendFormat("{0} => DbType = {1}, SourceColumn = {2}", param.ParameterName, param.DbType, param.SourceColumn); log.AppendLine(); } }
/// <summary> /// 创建IDBDatabase /// </summary> /// <param name="providerFactory">表示一组方法,这些方法用于创建提供程序对数据源类的实现的实例</param> /// <returns>创建的IDBDatabase</returns> public static IDBDatabase CreateDatabase(DbProviderFactory providerFactory) { DBProvider dbProvider = new DBProvider(); using (DbConnection cnn = providerFactory.CreateConnection()) using (DbCommand cmd = providerFactory.CreateCommand()) using (DbCommandBuilder cmdBuilder = providerFactory.CreateCommandBuilder()) using (DbDataAdapter adapter = providerFactory.CreateDataAdapter()) { DbParameter param = providerFactory.CreateParameter(); dbProvider.Connection = cnn.GetType(); dbProvider.Command = cmd.GetType(); dbProvider.Parameter = param.GetType(); dbProvider.CommandBuilder = cmdBuilder.GetType(); dbProvider.DataAdapter = adapter.GetType(); } IDBDatabase db = CreateDatabase(dbProvider); return(db); }
/// <summary> /// Crea un comando en base a una sentencia SQL o Nombre de un Store Procedure.Ejemplo: /// <code>SELECT * FROM Tabla WHERE campo1=@campo1, campo2=@campo2 o SP_MI_PROCEDURE</code> /// Guarda el comando para el seteo de parámetros y la posterior ejecución. /// </summary> /// <param name="sentenciaSQL">La sentencia SQL con el formato: SENTENCIA [param = @param,] o SP_MI_PROCEDURE</param> /// <param name="TipoComando">El Tipo del comando: TIPO StoredProcedure o Text</param> /// <param name="traerParametros">Determina si se van a traer de forma automatica los parametros en caso sea un procedimento</param> /// <param name="commandTimeout">Determina la cantidad de tiempo de espera de la ejecución del comando (segundos)</param> public void CrearComando(string sentenciaSQL, CommandType TipoComando, Boolean traerParametros = false, Int32 commandTimeout = 900) { this._Comando = _Factory.CreateCommand(); this._Comando.Connection = this._Conexion; this._Comando.CommandType = TipoComando; this._Comando.CommandText = sentenciaSQL; this._Comando.CommandTimeout = commandTimeout; if (traerParametros) { if (TipoComando == CommandType.StoredProcedure) { if (this._Conexion == null) { throw new BaseDatosException("La conexión no fue inicializada."); } if (!this._Conexion.State.Equals(ConnectionState.Open)) { throw new BaseDatosException("La conexión no fue abierta."); } try { DbCommandBuilder cmComando = _Factory.CreateCommandBuilder(); MethodInfo mi = cmComando.GetType().GetMethod("DeriveParameters"); ///''''''''''''''''''''''''''''''''''''' //Dim miconexion As New Oracle.DataAccess.Client.OracleConnection("Data Source=trondes.mapfreperu.com;Persist Security Info=True;User ID=tron2000;Password=tron2000;") //Dim micomando As New Oracle.DataAccess.Client.OracleCommand("select * from dual", miconexion) //micomando.CommandType = CommandType.Text //miconexion.Open() //Dim adapter As New Oracle.DataAccess.Client.OracleDataAdapter(micomando) //Dim dtTabla As New DataTable //adapter.Fill(dtTabla) //'Oracle.DataAccess.Client.OracleCommandBuilder.DeriveParameters(micomando) //'Dim adapter As New Oracle.DataAccess.Client.OracleDataAdapter("Data Source=trondes.mapfreperu.com;Persist Security Info=True;User ID=tron2000;Password=tron2000;", "gc_k_ws_bcos_mpe.p_bbva") //Dim builder As New Oracle.DataAccess.Client.OracleCommandBuilder(adapter) //'micomando.Parameters = ///'''''''''''''''''''''''''''''''''''''''' //Para asignar el derive parametes sin complicaciones por comenzar una transaccion if ((this._Transaccion != null)) { DbConnection cnConexion = null; cnConexion = _Factory.CreateConnection(); cnConexion.ConnectionString = _CadenaConexion; cnConexion.Open(); this._Comando.Connection = cnConexion; mi.Invoke(null, new object[] { this._Comando }); cnConexion.Close(); cnConexion.Dispose(); this._Comando.Connection = this._Conexion; } else { mi.Invoke(null, new object[] { this._Comando }); } } catch (TargetInvocationException ex) { throw new BaseDatosException("Error al crear el comando.", ex); } } } if ((this._Transaccion != null)) { this._Comando.Transaction = this._Transaccion; } }