/// <summary>Updates a data set.</summary> /// <param name="conn">The connection information.</param> /// <param name="insertCommand">The command for inserting rows.</param> /// <param name="updateCommand">The command for updating rows.</param> /// <param name="deleteCommand">The command for deleting rows.</param> /// <param name="ds">The data set to update.</param> /// <param name="table">The table to update.</param> /// <remarks> /// The default implementation uses a <see cref="DbDataAdapter"/> to update the data set using /// the provided commands. /// </remarks> protected virtual void UpdateDataSetCore(ConnectionData conn, DataCommand insertCommand, DataCommand updateCommand, DataCommand deleteCommand, DataSet ds, string table) { DbCommand cmdInsert = null, cmdUpdate = null, cmdDelete = null; try { conn.Open(); //Create the adapter using (DbDataAdapter da = CreateDataAdapterBase()) { if (insertCommand != null) { cmdInsert = PrepareCommandCore(conn, insertCommand); da.InsertCommand = cmdInsert; } ; if (updateCommand != null) { cmdUpdate = PrepareCommandCore(conn, updateCommand); da.UpdateCommand = cmdUpdate; } ; if (deleteCommand != null) { cmdDelete = PrepareCommandCore(conn, deleteCommand); da.DeleteCommand = cmdDelete; } ; //Update da.Update(ds, table); //Commit the changes ds.AcceptChanges(); }; } finally { if (cmdInsert != null) { cmdInsert.Dispose(); } if (cmdUpdate != null) { cmdUpdate.Dispose(); } if (cmdDelete != null) { cmdDelete.Dispose(); } }; }
/// <summary>Creates a command.</summary> /// <param name="command">The command.</param> /// <returns>The underlying command.</returns> protected abstract DbCommand CreateCommandBase(DataCommand command);
/// <summary>Executes a command and returns the results as a <see cref="DataSet"/>.</summary> /// <param name="command">The command to execute.</param> /// <returns>The results as a <see cref="DataSet"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception> public DataSet ExecuteDataSet(DataCommand command) { return(ExecuteDataSet(command, null)); }
/// <summary>Fills a data set with the results of a command.</summary> /// <param name="transaction">The transaction to execute within. If it is <see langword="null"/> then no transaction is used.</param> /// <param name="ds">The data set to fill.</param> /// <param name="command">The command to execute.</param> /// <exception cref="ArgumentNullException"><paramref name="ds"/> or <paramref name="command"/> is <see langword="null"/>.</exception> public void FillDataSet(DataCommand command, DataSet ds, DataTransaction transaction) { FillDataSet(command, ds, null, transaction); }
/// <summary>Updates a <see cref="DataSet"/>.</summary> /// <param name="insertCommand">Command to use for insertions.</param> /// <param name="deleteCommand">Command to use for deletions.</param> /// <param name="updateCommand">Command to use for updates.</param> /// <param name="ds"><see cref="DataSet"/> to use.</param> /// <param name="table">The table to use when updating the command. If <see langword="null"/> or empty then the /// first table is used.</param> /// <remarks> /// To support transactions, precede this call with ExecuteScalar("begin transaction"). Failure to /// use transactions could cause failed updates to be partially applied. /// <para/> /// The command parameters can be <see langword="null"/> but the update will fail if the command is needed. /// </remarks> /// <exception cref="DBConcurrencyException">A concurrency error occurred.</exception> /// <exception cref="ArgumentNullException"><paramref name="ds"/> is <see langword="null"/>.</exception> public void UpdateDataSet(DataCommand insertCommand, DataCommand deleteCommand, DataCommand updateCommand, DataSet ds, string table) { UpdateDataSet(insertCommand, deleteCommand, updateCommand, ds, table, null); }
/// <summary>Fills a data set with the results of a command.</summary> /// <param name="ds">The data set to fill.</param> /// <param name="command">The command to execute.</param> /// <exception cref="ArgumentNullException"><paramref name="ds"/> or <paramref name="command"/> is <see langword="null"/>.</exception> public void FillDataSet(DataCommand command, DataSet ds) { FillDataSet(command, ds, (DataTransaction)null); }
/// <summary>Fills a data set with the results of a command.</summary> /// <param name="ds">The data set to fill.</param> /// <param name="tables">The tables to use for storing the results.</param> /// <param name="command">The command to execute.</param> /// <exception cref="ArgumentNullException"><paramref name="ds"/> or <paramref name="command"/> is <see langword="null"/>.</exception> public void FillDataSet(DataCommand command, DataSet ds, string[] tables) { FillDataSet(command, ds, tables, null); }
/// <summary>Executes a command and returns the first result.</summary> /// <typeparam name="T">The type of the value returned.</typeparam> /// <param name="transaction">The transaction to execute within. If it is <see langword="null"/> then no transaction is used.</param> /// <param name="command">The command to execute.</param> /// <returns>The first element from the result set.</returns> /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception> public T ExecuteScalar <T>(DataCommand command, DataTransaction transaction) { var result = ExecuteScalar(command, transaction); return(((result != null) && (result != DBNull.Value)) ? (T)Convert.ChangeType(result, typeof(T)) : default(T)); }
/// <summary>Executes a command and returns the first result.</summary> /// <param name="command">The command to execute.</param> /// <returns>The first element from the result set.</returns> /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception> public object ExecuteScalar(DataCommand command) { return(ExecuteScalar(command, null)); }
/// <summary>Executes a command and returns a data reader with the results.</summary> /// <param name="command">The command to execute.</param> /// <returns>A <see cref="DbDataReader"/> containing the results. The reader may be empty but it will /// never be <see langword="null"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception> public DbDataReader ExecuteReader(DataCommand command) { return(ExecuteReader(command, null)); }
/// <summary>Executes a command, parses the first result and returns a strongly-typed object.</summary> /// <typeparam name="TResult">The type of the objects to return.</typeparam> /// <param name="command">The command to execute.</param> /// <param name="converter">The method to convert the row to an object.</param> /// <param name="data">The data to pass to the converter.</param> /// <returns>An object containing the data that was parsed.</returns> /// <exception cref="ArgumentNullException"><paramref name="command"/> or <paramref name="converter"/> is <see langword="null"/>.</exception> /// <remarks> /// This method combines the functionality of <see cref="O:ExecuteReader"/> with the standard code used to load data /// from a reader into a business object. <paramref name="converter"/> is called once for each row to build the list /// of data objects to return. /// </remarks> /// <seealso cref="O:ExecuteQueryWithResults{TResult}"/> public TResult ExecuteQueryWithResult <TResult>(DataCommand command, Func <DbDataReader, object, TResult> converter, object data) { return(ExecuteQueryWithResult <TResult>(command, converter, data, null)); }
/// <summary>Executes a command and returns the number of affected rows.</summary> /// <param name="command">The command to execute.</param> /// <returns>The number of rows affected.</returns> /// <exception cref="ArgumentNullException"><paramref name="command"/> is <see langword="null"/>.</exception> public int ExecuteNonQuery(DataCommand command) { return(ExecuteNonQuery(command, null)); }