WEB开发网
开发学院数据库MSSQL Server ORM: 开发自己的Data Access Application Block -... 阅读

ORM: 开发自己的Data Access Application Block - Part II

 2008-12-06 10:15:42 来源:WEB开发网   
核心提示: Part III Execute 系列using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.Common;using Artech.Applic

Part III Execute 系列

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
  
using Artech.ApplicationBlock.DataMapping;
  
namespace Artech.ApplicationBlock.DataAccess
{
  /**//// <summary>
  /// Database defines a series of database-based operations.
  /// </summary>
  public abstract partial class Database : IDisposable
  {
  
    ExecuteNonQuery#region ExecuteNonQuery
    public int ExecuteNonQuery(CommandType commandType, string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
    {
      DbCommand command = this.Connection.CreateCommand();
      command.CommandType = commandType;
      command.CommandText = commandText;
  
      DbParameter parameter;
      foreach (string parameterName in inputParameters.Keys)
      {
        parameter = this._dbProviderFactory.CreateParameter();
        parameter.ParameterName = parameterName;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = inputParameters[parameterName];
        command.Parameters.Add(parameter);
      }
  
      foreach (string parameterName in outputParameters.Keys)
      {
        parameter = this._dbProviderFactory.CreateParameter();
        parameter.ParameterName = parameterName;
        parameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(parameter);
      }
  
      this.Connection.Open();
      int rowCount = command.ExecuteNonQuery();
      this.Connection.Close();
  
      foreach (string parameterName in outputParameters.Keys)
      {
        outputParameters[parameterName] = command.Parameters[parameterName];
      }
  
      return rowCount;
    }
  
    public int ExecuteNonQuery(CommandType commandType, string commandText, Dictionary<string, object> inputParameters)
    {
      return this.ExecuteNonQuery(commandType, commandText, inputParameters, new Dictionary<string, object>());
    }
  
    public int ExecuteNonQuery(string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
    {
      return this.ExecuteNonQuery(this._defaultCommandType, commandText, inputParameters, outputParameters);
    }
  
    public int ExecuteNonQuery(string commandText, Dictionary<string, object> inputParameters)
    {
      return this.ExecuteNonQuery(this._defaultCommandType, commandText, inputParameters, new Dictionary<string, object>());
    }
    #endregion
  
    ExecuteReader#region ExecuteReader
    public DbDataReader ExecuteReader(CommandType commandType, string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
    {
      DbCommand command = this.Connection.CreateCommand();
      command.CommandType = commandType;
      command.CommandText = commandText;
  
      DbParameter parameter;
      foreach (string parameterName in inputParameters.Keys)
      {
        parameter = this._dbProviderFactory.CreateParameter();
        parameter.ParameterName = parameterName;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = inputParameters[parameterName];
        command.Parameters.Add(parameter);
      }
  
      foreach (string parameterName in outputParameters.Keys)
      {
        parameter = this._dbProviderFactory.CreateParameter();
        parameter.ParameterName = parameterName;
        parameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(parameter);
      }
  
      this.Connection.Open();
      DbDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
  
      foreach (string parameterName in outputParameters.Keys)
      {
        outputParameters[parameterName] = command.Parameters[parameterName];
      }
  
      return dataReader;
    }
  
    public DbDataReader ExecuteReader(CommandType commandType, string commandText, Dictionary<string, object> inputParameters)
    {
      return this.ExecuteReader(commandType, commandText, inputParameters, new Dictionary<string, object>());
    }
  
    public DbDataReader ExecuteReader(string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
    {
      return this.ExecuteReader(this._defaultCommandType, commandText, inputParameters, outputParameters);
    }
  
    public DbDataReader ExecuteReader(string commandText, Dictionary<string, object> inputParameters)
    {
      return this.ExecuteReader(this._defaultCommandType, commandText, inputParameters, new Dictionary<string, object>());
    }
    #endregion
  
    ExecuteScalar#region ExecuteScalar
    public object ExecuteScalar(CommandType commandType, string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
    {
      DbCommand command = this.Connection.CreateCommand();
      command.CommandType = commandType;
      command.CommandText = commandText;
  
      DbParameter parameter;
      foreach (string parameterName in inputParameters.Keys)
      {
        parameter = this._dbProviderFactory.CreateParameter();
        parameter.ParameterName = parameterName;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = inputParameters[parameterName];
        command.Parameters.Add(parameter);
      }
  
      foreach (string parameterName in outputParameters.Keys)
      {
        parameter = this._dbProviderFactory.CreateParameter();
        parameter.ParameterName = parameterName;
        parameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(parameter);
      }
  
      this.Connection.Open();
      object returnValue = command.ExecuteScalar();
      this.Connection.Close();
  
      foreach (string parameterName in outputParameters.Keys)
      {
        outputParameters[parameterName] = command.Parameters[parameterName];
      }
  
      return returnValue;
    }
  
    public object ExecuteScalar(CommandType commandType, string commandText, Dictionary<string, object> inputParameters)
    {
      return this.ExecuteScalar(commandType, commandText, inputParameters, new Dictionary<string, object>());
    }
  
    public object ExecuteScalar(string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
    {
      return this.ExecuteScalar(this._defaultCommandType, commandText, inputParameters, outputParameters);
    }
  
    public object ExecuteScalar(string commandText, Dictionary<string, object> inputParameters)
    {
      return this.ExecuteScalar(this._defaultCommandType, commandText, inputParameters, new Dictionary<string, object>());
    }
    #endregion
  }
}

上一页  1 2 3 

Tags:ORM 开发 自己

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接