internal DataTable GetDataTable(string sqlCommand, DBParameterCollection paramCollection, IDbConnection connection, string tableName, CommandType commandType) { DataTable table = null; if (tableName != string.Empty) { table = new DataTable(tableName); } else { table = new DataTable(); } IDbCommand command = null; if (paramCollection != null) { if (paramCollection.Parameters.Count > 0) { command = _commandBuilder.GetCommand(sqlCommand, connection, paramCollection, commandType); } else { command = _commandBuilder.GetCommand(sqlCommand, connection, commandType); } } else { command = _commandBuilder.GetCommand(sqlCommand, connection, commandType); } DbDataAdapter adapter = GetDataAdapter(command); ConstructorInfo constructor = adapter.GetType().GetConstructor(new Type[] { command.GetType() }); adapter = (DbDataAdapter)constructor.Invoke(new object[] { command }); MethodInfo method = adapter.GetType().GetMethod("Fill", new Type[] { typeof(DataTable) }); try { method.Invoke(adapter, new object[] { table }); } catch (Exception err) { throw err; } return(table); }
public DataTable DBProbe(string sSQL) { DataTable DT = new DataTable(); try { SQLSpecific.OpenConn(sql_con); sql_cmd = sql_con.CreateCommand(); sql_cmd.CommandText = sSQL; sql_da.SelectCommand = sql_cmd; int i = sql_da.Fill(DT); string s = "Database:" + sql_con.Database + ", DataSource:" + sql_con.DataSource.ToString() + ", ServerVersion:" + sql_con.ServerVersion; DBDescStr = sql_da.GetType().FullName + "; " + s + " (" + sql_con.ConnectionString + ")"; } catch (Exception caught) { DT = null; DBErrorStr = caught.Message; DBMain.AltLog(LogLevels.Warning, 70193, "DBProbe '" + caught.Message + "'"); } if (sql_con != null) { sql_con.Close(); } return(DT); }
public static void AddRowUpdatingHandler(this DbDataAdapter adapter, RowUpdatingEventHandler handler) { EventInfo updEvent = adapter.GetType().GetEvent("RowUpdating", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); if (updEvent != null) { updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Method)); } }
public DbDataAdapter GetAdapter(DbCommand SelectCommand) { DbDataAdapter objAdapter = null; System.Type objType = null; System.Reflection.PropertyInfo objPropertyInfo = null; try { InitializeAdapter(ref objAdapter); if (objAdapter != null) { if (SelectCommand != null) { //SelectCommand.Connection = this.Connection; objType = objAdapter.GetType(); if (objType != null) { //((object)objAdapter).SelectCommand = SelectCommand; objPropertyInfo = objType.GetProperty("SelectCommand", SelectCommand.GetType()); objPropertyInfo.SetValue(objAdapter, SelectCommand, null); } } } } catch (Exception ex) { throw new Exception("GetAdapter: " + ex.Message); } finally { if (objType != null) { objType = null; } if (objPropertyInfo != null) { objPropertyInfo = null; } } return(objAdapter); }
/// <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> /// Registers a <c>RowUpdatingEventHandler</c> with this instance's <c>RowUpdating</c> event. /// </summary> /// <param name="handler">The event handler to register for the event.</param> /// <returns><c>true</c> if the event handler was successfully registered, otherwise <c>false</c>.</returns> public static bool AddRowUpdatingHandler(this DbDataAdapter adapter, RowUpdatingEventHandler handler) { EventInfo updEvent = GetEvent("RowUpdating", adapter.GetType()); if (updEvent != null) { try { if (handler.Method.IsStatic) { updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Method)); } else { updEvent.AddEventHandler(adapter, Delegate.CreateDelegate(updEvent.EventHandlerType, handler.Target, handler.Method)); } return(true); } catch { } } return(false); }
/// <summary> /// Creates the necessary components associated with this data adapter instance /// </summary> /// <param name="host">The designer host</param> /// <returns>The components created by this toolbox item</returns> protected override IComponent[] CreateComponentsCore(IDesignerHost host) { DbProviderFactory fact = DbProviderFactories.GetFactory("Npgsql"); DbDataAdapter dataAdapter = fact.CreateDataAdapter(); IContainer container = host.Container; using (DbCommand adapterCommand = fact.CreateCommand()) { adapterCommand.DesignTimeVisible = false; dataAdapter.SelectCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.SelectCommand, GenerateName(container, "SelectCommand")); dataAdapter.InsertCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.InsertCommand, GenerateName(container, "InsertCommand")); dataAdapter.UpdateCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.UpdateCommand, GenerateName(container, "UpdateCommand")); dataAdapter.DeleteCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.DeleteCommand, GenerateName(container, "DeleteCommand")); } ITypeResolutionService typeResService = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService)); if (typeResService != null) { typeResService.ReferenceAssembly(dataAdapter.GetType().Assembly.GetName()); } container.Add(dataAdapter); List <IComponent> list = new List <IComponent>(); list.Add(dataAdapter); // Show the connection wizard if we have a type for it if (_wizard != null) { using (Form wizard = (Form)Activator.CreateInstance(_wizard, new object[] { host, dataAdapter })) { wizard.ShowDialog(); } } if (dataAdapter.SelectCommand != null) { list.Add(dataAdapter.SelectCommand); } if (dataAdapter.InsertCommand != null) { list.Add(dataAdapter.InsertCommand); } if (dataAdapter.DeleteCommand != null) { list.Add(dataAdapter.DeleteCommand); } if (dataAdapter.UpdateCommand != null) { list.Add(dataAdapter.UpdateCommand); } return(list.ToArray()); }
/// <summary> /// Creates the db data adapter. /// </summary> /// <returns></returns> private DbDataAdapter createDbDataAdapter() { DbDataAdapter ret = connectionInfo.ProviderFactory.CreateDataAdapter(); #if (DEBUG) Log.Instance.AddFormat("[StoredProcedure] Created DbDataAdapter of type \"{0}\"", ret.GetType(), LogLevel.Debug); #endif ret.SelectCommand = commandWrapper.DbCommand; return(ret); }