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.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
}
}
- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››自己动手写iPhone wap浏览器之界面架构篇
- ››自己也能DIY个性真人QQ表情
- ››自己动手!巧法让酷狗动感歌词更完美
- ››自己编译Google Android内核的Linux源码
- ››自己写的一个jquery模板引擎(json比较好用)
- ››开发一个自己的HTML在线编辑器(一)
- ››开发一个自己的HTML在线编辑器(二)
更多精彩
赞助商链接