コード例 #1
0
        public IDataCommand CreateCommand(IDataCommandTemplate commandTemplate, DataParameterValue parameterValue)
        {
            IDbCommand command = Connection().CreateCommand();

            command.Connection  = UnderlyingConnection();
            command.CommandType = commandTemplate.CommandType;
            command.CommandText = commandTemplate.CommandText;

            foreach (IDataParameterTemplate parameterTemplate in commandTemplate.Parameters)
            {
                IDbDataParameter parameter = command.CreateParameter();
                parameter.ParameterName = parameterTemplate.Name;

                // AdomdParamater has not implemented the property DbType - awesome
                if (!parameter.GetType().Equals(typeof(AdomdParameter)))
                {
                    parameter.DbType = parameterTemplate.DbType;
                }

                parameter.Value = parameterValue(parameterTemplate);

                command.Parameters.Add(parameter);
            }

            return(new DataCommand(command));
        }
コード例 #2
0
ファイル: SimpleQuery.cs プロジェクト: vivsrane/TestReports
        public static NonQueryResult ExecuteNonQuery(IDataConnection connection, IDataCommandTemplate commandTemplate, DataParameterValue parameterValue, bool collectPrimaryKey)
        {
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            using (IDataCommand command = connection.CreateCommand(commandTemplate, parameterValue))
            {
                using (IDbTransaction transaction = connection.BeginTransaction())
                {
                    command.Transaction = transaction;

                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected != 1)
                    {
                        throw new DataException("Number of rows affected = " + rowsAffected + " expected 1");
                    }

                    object primaryKey;

                    if (collectPrimaryKey)
                    {
                        DataCommandTemplate identity = new DataCommandTemplate(CommandType.Text, "SELECT @@IDENTITY Id");

                        using (IDbCommand primaryKeyCommand = connection.CreateCommand(identity, parameterValue))
                        {
                            primaryKeyCommand.Transaction = transaction;

                            primaryKey = primaryKeyCommand.ExecuteScalar();
                        }
                    }
                    else
                    {
                        primaryKey = null;
                    }

                    transaction.Commit();

                    return(new NonQueryResult(primaryKey, rowsAffected));
                }
            }
        }