コード例 #1
0
        /// <summary>
        ///     Creates the parameterized query.
        /// </summary>
        /// <param name="source">The nodes.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="commandTextType">Type of the command text.</param>
        /// <returns>
        ///     Returns a <see cref="String" /> representing the parameterized query.
        /// </returns>
        public static string CreateCommandText(this DbParameterCollection source, string tableName, CommandTextType commandTextType)
        {
            if (source == null)
            {
                return(null);
            }

            string commandText;

            if (commandTextType == CommandTextType.InsertCommand)
            {
                string values  = string.Join(", ", source.Cast <DbParameter>().Select(o => ":" + o.ParameterName).ToArray());
                string columns = string.Join(", ", source.Cast <DbParameter>().Select(o => o.ParameterName).ToArray());
                commandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableName, columns, values);
            }
            else if (commandTextType == CommandTextType.UpdateCommand)
            {
                string values = string.Join(", ", source.Cast <DbParameter>().Select(o => o + " = :" + o.ParameterName).ToArray());
                commandText = string.Format("UPDATE {0} SET {1}", tableName, values);
            }
            else
            {
                string values = string.Join(" AND ", source.Cast <DbParameter>().Select(o => o + " = :" + o.ParameterName).ToArray());
                commandText = string.Format("DELETE FROM {0} WHERE {1}", tableName, values);
            }

            foreach (DbParameter parameter in source)
            {
                parameter.ParameterName = string.Format(":{0}", parameter.ParameterName.ToUpperInvariant());
            }

            return(commandText);
        }
コード例 #2
0
 /// <summary>
 ///     Executes the given INSERT, UPDATE or DELETE statement and returns the number of rows affected.
 /// </summary>
 /// <param name="source">The source.</param>
 /// <param name="tableName">Name of the table.</param>
 /// <param name="commandTextType">Type of the command text.</param>
 /// <param name="transaction">The transaction.</param>
 /// <param name="addWithValue">The add with value.</param>
 /// <returns>
 ///     The number of rows affected.
 /// </returns>
 public static int ExecuteNonQuery(this DbConnection source, string tableName, CommandTextType commandTextType, DbTransaction transaction, Action <DbParameterCollection> addWithValue)
 {
     return(source.ExecuteNonQuery(tableName, commandTextType, transaction, addWithValue, collection => collection.CreateCommandText(tableName, commandTextType)));
 }
コード例 #3
0
        /// <summary>
        /// Executes the given INSERT, UPDATE or DELETE statement and returns the number of rows affected.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="commandTextType">Type of the command text.</param>
        /// <param name="transaction">The transaction.</param>
        /// <param name="addWithValue">The add with value.</param>
        /// <param name="commandText">The command text.</param>
        /// <returns>
        /// The number of rows affected.
        /// </returns>
        public static int ExecuteNonQuery(this DbConnection source, string tableName, CommandTextType commandTextType, DbTransaction transaction, Action <DbParameterCollection> addWithValue, Func <DbParameterCollection, string> commandText)
        {
            using (DbCommand cmd = source.CreateCommand())
            {
                addWithValue(cmd.Parameters);

                cmd.CommandText = commandText(cmd.Parameters);
                cmd.CommandType = CommandType.Text;
                cmd.Transaction = transaction;

                return(cmd.ExecuteNonQuery());
            }
        }
コード例 #4
0
 /// <summary>
 /// Executes the given INSERT, UPDATE or DELETE statement and returns the number of rows affected.
 /// </summary>
 /// <param name="source">The source.</param>
 /// <param name="tableName">Name of the table.</param>
 /// <param name="commandTextType">Type of the command text.</param>
 /// <param name="addWithValue">The add with value.</param>
 /// <param name="commandText">The command text.</param>
 /// <returns>
 /// The number of rows affected.
 /// </returns>
 public static int ExecuteNonQuery(this DbTransaction source, string tableName, CommandTextType commandTextType, Action <DbParameterCollection> addWithValue, Func <DbParameterCollection, string> commandText)
 {
     return(source.Connection.ExecuteNonQuery(tableName, commandTextType, source, addWithValue, commandText));
 }