Exemple #1
0
        private static void PopulateReturnParameters(SqlCommand cmd, DataCommand command, DataResult result)
        {
            if (((cmd.Parameters.Count > 0) && (command.Parameters != null)) && (command.Parameters.Count > 0))
            {
                result.Parameters = new DataParameters();
                int index = 0;
                foreach (DataParameter param in command.Parameters)
                {
                    if (param.Direction != ParameterDirection.Input)
                    {
                        result.Parameters.Add(param);
                        SqlParameter p = null;
                        if (cmd.CommandType == CommandType.StoredProcedure)
                        {
                            p = cmd.Parameters["@" + param.ParamName];
                        }
                        else
                        {
                            p = cmd.Parameters["@" + "p" + index.ToString()];
                        }

                        param.Value = p.Value;
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Retiurns DataRow[]
        /// </summary>
        /// <param name="command"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        private DataResult SaveInternal(DataCommand command, DataRow[] rows)
        {
            bool inserts = false;
            bool updates = false;
            bool deletes = false;

            if (rows.Length > 0)
            {
                // Do we have any data to save
                foreach (DataRow row in rows)
                {
                    switch (row.RowState)
                    {
                    case DataRowState.Added:
                        inserts = true;
                        break;

                    case DataRowState.Modified:
                        updates = true;
                        break;

                    case DataRowState.Deleted:
                        deletes = true;
                        break;
                    }
                }
            }

            // Use the command connection string if one specified, otherwise use the one configured for the provider
            string connectionString = String.IsNullOrEmpty(command.ConnectionString) ? this._ConnectionString : command.ConnectionString;
            int    rowsEffected     = 0;

            using (TransactionScope tran = new TransactionScope())
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter();

                if (command.TableMapping != null && command.TableMapping.Length > 0)
                {
                    if (inserts)
                    {
                        dataAdapter.InsertCommand = command.Parameters.Count > 0 ? this.CreateInsertCommand(command) : this.CreateInsertCommand(command.Table, GetTableNameFromMapping(command.TableMapping, rows[0].Table.TableName));
                    }
                    if (updates)
                    {
                        dataAdapter.UpdateCommand = command.Parameters.Count > 0 ? this.CreateUpdateCommand(command) : this.CreateUpdateCommand(command.Table, GetTableNameFromMapping(command.TableMapping, rows[0].Table.TableName));
                    }
                    if (deletes)
                    {
                        dataAdapter.DeleteCommand = command.Parameters.Count > 0 ? this.CreateDeleteCommand(command) : this.CreateDeleteCommand(command.Table, GetTableNameFromMapping(command.TableMapping, rows[0].Table.TableName));
                    }
                }
                else
                {
                    if (inserts)
                    {
                        dataAdapter.InsertCommand = command.Parameters.Count > 0 ? this.CreateInsertCommand(command) : this.CreateInsertCommand(command.Table);
                    }
                    if (updates)
                    {
                        dataAdapter.UpdateCommand = command.Parameters.Count > 0 ? this.CreateUpdateCommand(command) : this.CreateUpdateCommand(command.Table);
                    }
                    if (deletes)
                    {
                        dataAdapter.DeleteCommand = command.Parameters.Count > 0 ? this.CreateDeleteCommand(command) : this.CreateDeleteCommand(command.Table);
                    }
                }

                if (inserts)
                {
                    TransactionScope.Enlist(dataAdapter.InsertCommand, connectionString, new TransactionScope.ConnectionDelegate(CreateConnectionDelegate));
                }
                if (updates)
                {
                    TransactionScope.Enlist(dataAdapter.UpdateCommand, connectionString, new TransactionScope.ConnectionDelegate(CreateConnectionDelegate));
                }
                if (deletes)
                {
                    TransactionScope.Enlist(dataAdapter.DeleteCommand, connectionString, new TransactionScope.ConnectionDelegate(CreateConnectionDelegate));
                }

                rowsEffected = dataAdapter.Update(rows);

                if (inserts)
                {
                    TransactionScope.DeEnlist(dataAdapter.InsertCommand);
                }
                if (updates)
                {
                    TransactionScope.DeEnlist(dataAdapter.UpdateCommand);
                }
                if (deletes)
                {
                    TransactionScope.DeEnlist(dataAdapter.DeleteCommand);
                }

                tran.Complete();
            }

            DataResult result = new DataResult();

            result.DataRows     = rows;
            result.RowsEffected = rowsEffected;

            return(result);
        }