转载自http://www.cnblogs.com/artech/archive/2007/04/03/698762.html
4. Database
下面来介绍重中之重:Database,绝大部分的Data Access 操作都集中在这个Abstract Database中。这是一个相对庞大的Class,所以不得不采用Partial Class的方式来编写。
Part I:Field 和Property
这些Field 和Property基本上对应我们前面的Configuraiton。此为我们定义了三个Field 和Property:DbDataAdapter,Connection,_transaction。考虑到垃圾回收,使Database实现IDisposable接口。值得说明一点的是,我们通过Database的DatabaseProviderFactory创建了泛型的DbDataAdapter,DbConnection和Transaction。
-
ConnectionString:string
-
DatabaseProviderFactory:DbProviderFactory
-
DefaultCommandType:CommandType
-
UseCommandBuilder:bool
-
DbParameterNameMapping:IDbParameterNameMapping
-
StoredProcedureNameMapping:IStoredProcedureNameMapping
-
DbDataAdapter:DbDataAdapter
-
Connection: DbConnection
-
Transaction: DbTransaction
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using Artech.ApplicationBlock.DataMapping;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Artech.ApplicationBlock.DataAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Database defines a series of database-based operations.
/// </summary>
public abstract partial class Database : IDisposable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private bool _isDisposed;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
The five private fields possess the corressponding pubic properties, and they are only allowed to be evaluated by Database Factory.#region The five private fields possess the corressponding pubic properties, and they are only allowed to be evaluated by Database Factory.
private DbProviderFactory _dbProviderFactory;
private string _connectionString;
private CommandType _defaultCommandType;
private bool _useCommandBuilder;
private IDbParameterNameMapping _dbParameterNameMapping;
private IStoredProcedureNameMapping _storedProcedureNameMapping;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Database connection string which is specified by the database factory.
/// </summary>
public string ConnectionString
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._connectionString;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._connectionString = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The concrete database specific provider factory.
/// </summary>
public DbProviderFactory DatabaseProviderFactory
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dbProviderFactory;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._dbProviderFactory = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The defaull command type to perform the database operations which do not specify the commanf type.
/// </summary>
public CommandType DefaultCommandType
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._defaultCommandType;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._defaultCommandType = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///Determine whether to use command builder or mapped stored procedures to execute database operations.
/// </summary>
public bool UseCommandBuilder
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._useCommandBuilder;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._useCommandBuilder = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// A string which indicates the type to perform mapping between stored procedure parameter and source column.
/// </summary>
public IDbParameterNameMapping DbParameterNameMapping
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dbParameterNameMapping;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._dbParameterNameMapping = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// A string which indicates the type to perform mapping between table name and the related stored procedure names.
/// </summary>
public IStoredProcedureNameMapping StoredProcedureNameMapping
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._storedProcedureNameMapping;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._storedProcedureNameMapping = value;
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Connection & Database DataAdapter#region Connection & Database DataAdapter
private DbDataAdapter _dbDataAdapter;
private DbConnection _connection;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// A generic database data adapter which is responsible for save the changed data into database.
/// </summary>
private DbDataAdapter DatabaseAdapter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._dbDataAdapter == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._dbDataAdapter = this._dbProviderFactory.CreateDataAdapter();
this._dbDataAdapter.AcceptChangesDuringUpdate = false;
this._dbDataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return this._dbDataAdapter;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The database connection.
/// </summary>
private DbConnection Connection
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._connection == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._connection = this._dbProviderFactory.CreateConnection();
this._connection.ConnectionString = this._connectionString;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return this._connection;
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructor#region Constructor
public Database()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IDisposable Members#region IDisposable Members
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dispose(true);
GC.SuppressFinalize(this);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Dispose(bool disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!this._isDisposed)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._connection != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._connection.State == ConnectionState.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._connection.Close();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this._transaction != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._transaction.Dispose();
}
}
}
this._isDisposed = true;
}
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Part II: Fill Dataset
很简单,基本上ADO.NET 的基本操作,没什么可值得说的。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using Artech.ApplicationBlock.DataMapping;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Artech.ApplicationBlock.DataAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Database defines a series of database-based operations.
/// </summary>
public abstract partial class Database : IDisposable,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void FillDataSet(DataSet dataInfo, string commandText, IDictionary<string, object> parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (dataInfo.Tables.Count == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataTable table = dataInfo.Tables.Add("Table1");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.FillDataSet(dataInfo, dataInfo.Tables[0].TableName, this._defaultCommandType, commandText, parameters);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void FillDataSet(DataSet dataInfo, string tableName, string commandText, IDictionary<string, object> parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.FillDataSet(dataInfo, tableName, this._defaultCommandType, commandText, parameters);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void FillDataSet(DataSet dataInfo, string tableName, CommandType commandType, string commandText, IDictionary<string, object> parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand selectComand = this.Connection.CreateCommand();
selectComand.CommandText = commandText;
selectComand.CommandType = commandType;
this.DatabaseAdapter.SelectCommand = selectComand;
this.DatabaseAdapter.Fill(dataInfo, tableName);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Part III Execute 系列
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using Artech.ApplicationBlock.DataMapping;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Artech.ApplicationBlock.DataAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Database defines a series of database-based operations.
/// </summary>
public abstract partial class Database : IDisposable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ExecuteNonQuery#region ExecuteNonQuery
public int ExecuteNonQuery(CommandType commandType, string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand command = this.Connection.CreateCommand();
command.CommandType = commandType;
command.CommandText = commandText;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DbParameter parameter;
foreach (string parameterName in inputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter = this._dbProviderFactory.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Direction = ParameterDirection.Input;
parameter.Value = inputParameters[parameterName];
command.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in outputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter = this._dbProviderFactory.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Direction = ParameterDirection.Output;
command.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Connection.Open();
int rowCount = command.ExecuteNonQuery();
this.Connection.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in outputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
outputParameters[parameterName] = command.Parameters[parameterName];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return rowCount;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int ExecuteNonQuery(CommandType commandType, string commandText, Dictionary<string, object> inputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteNonQuery(commandType, commandText, inputParameters, new Dictionary<string, object>());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int ExecuteNonQuery(string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteNonQuery(this._defaultCommandType, commandText, inputParameters, outputParameters);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int ExecuteNonQuery(string commandText, Dictionary<string, object> inputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteNonQuery(this._defaultCommandType, commandText, inputParameters, new Dictionary<string, object>());
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ExecuteReader#region ExecuteReader
public DbDataReader ExecuteReader(CommandType commandType, string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand command = this.Connection.CreateCommand();
command.CommandType = commandType;
command.CommandText = commandText;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DbParameter parameter;
foreach (string parameterName in inputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter = this._dbProviderFactory.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Direction = ParameterDirection.Input;
parameter.Value = inputParameters[parameterName];
command.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in outputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter = this._dbProviderFactory.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Direction = ParameterDirection.Output;
command.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Connection.Open();
DbDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in outputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
outputParameters[parameterName] = command.Parameters[parameterName];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return dataReader;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbDataReader ExecuteReader(CommandType commandType, string commandText, Dictionary<string, object> inputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteReader(commandType, commandText, inputParameters, new Dictionary<string, object>());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbDataReader ExecuteReader(string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteReader(this._defaultCommandType, commandText, inputParameters, outputParameters);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbDataReader ExecuteReader(string commandText, Dictionary<string, object> inputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteReader(this._defaultCommandType, commandText, inputParameters, new Dictionary<string, object>());
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ExecuteScalar#region ExecuteScalar
public object ExecuteScalar(CommandType commandType, string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand command = this.Connection.CreateCommand();
command.CommandType = commandType;
command.CommandText = commandText;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DbParameter parameter;
foreach (string parameterName in inputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter = this._dbProviderFactory.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Direction = ParameterDirection.Input;
parameter.Value = inputParameters[parameterName];
command.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in outputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter = this._dbProviderFactory.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Direction = ParameterDirection.Output;
command.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Connection.Open();
object returnValue = command.ExecuteScalar();
this.Connection.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in outputParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
outputParameters[parameterName] = command.Parameters[parameterName];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return returnValue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public object ExecuteScalar(CommandType commandType, string commandText, Dictionary<string, object> inputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteScalar(commandType, commandText, inputParameters, new Dictionary<string, object>());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public object ExecuteScalar(string commandText, Dictionary<string, object> inputParameters, Dictionary<string, object> outputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteScalar(this._defaultCommandType, commandText, inputParameters, outputParameters);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public object ExecuteScalar(string commandText, Dictionary<string, object> inputParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ExecuteScalar(this._defaultCommandType, commandText, inputParameters, new Dictionary<string, object>());
}
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Part IV: Transaction
定义了3个基于Transaction的方法BeginTransaction,Commit和RollBack,使Developer显示地开始和结束一个Transaction,这样他可以很直观地把所需的操作纳入某个Transaction中。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using Artech.ApplicationBlock.DataMapping;
using Artech.ApplicationBlock.Common;
using Artech.ApplicationBlock.Common.Exceptions;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Artech.ApplicationBlock.DataAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Database defines a series of database-based operations.
/// </summary>
public abstract partial class Database : IDisposable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbTransaction _transaction;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Transaction based operation#region Transaction based operation
public void BeginTransaction()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.Connection.State != ConnectionState.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Connection.Open();
}
this._transaction = this.Connection.BeginTransaction();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Commit()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._transaction == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new DataAccessException(CommonConstant.DataAccess.Messages.NotBegunTransaction);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this._transaction.Commit();
}
public void RollBack()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._transaction == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new DataAccessException(CommonConstant.DataAccess.Messages.NotBegunTransaction);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this._transaction.Rollback();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbTransaction Transaction
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._transaction;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._transaction = value;
}
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Part V: Update
这一部分花了很多时间和精力,现看Code:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using Artech.ApplicationBlock.DataMapping;
using Artech.ApplicationBlock.Common;
using Artech.ApplicationBlock.Common.Exceptions;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Artech.ApplicationBlock.DataAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Database defines a series of database-based operations.
/// </summary>
public abstract partial class Database : IDisposable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Save the changed data hold in DataSet into database.#region Save the changed data hold in DataSet into database.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Discovery all of the parameters of the command.
/// </summary>
/// <param name="command"></param>
public abstract void DiscoverParameters(DbCommand command);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Get the approprate source version based on the parameter name. With the help of the source version, the database data adapter can choose the data of
/// appropriate version to save into underlying database.
/// </summary>
/// <param name="parameterName"></param>
/// <returns>The source version corresponding to the parameter.</returns>
public abstract DataRowVersion GetSourceVersion(string parameterName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save all of the changed data hold in a dataset into database.
/// </summary>
/// <param name="dataInfo">The dataset which contains all the changed data.</param>
public void UpdateData(DataSet dataInfo)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Deleted data.
if (dataInfo.GetChanges(DataRowState.Deleted) != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataTable table in dataInfo.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDependentTable(table, DataRowState.Deleted);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Modified Data.
if (dataInfo.GetChanges(DataRowState.Modified) != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataTable table in dataInfo.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDependentTable(table, DataRowState.Modified);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Added Data.
if (dataInfo.GetChanges(DataRowState.Added) != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataTable table in dataInfo.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDependentTable(table, DataRowState.Added);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save all of the changed data hold in a data table into database.
/// </summary>
/// <param name="dataInfo">The data table which contains all the changed data.</param>
public void UpdateData(DataTable table)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._useCommandBuilder)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDataUsingCommandBuilder(table);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDataUsingMappedStoredProcedure(table);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed data stored in a data table by specifying the command text and parameter to a series of commands - InsertCommand, UpdateCommand and DeleteCommand.
/// </summary>
/// <param name="table">The data table which stores the changed data.</param>
/// <param name="insertCommandText">The command text for insert command.</param>
/// <param name="updateCommandText">The command text for update command.</param>
/// <param name="deleteCommandText">The command text for delete command</param>
/// <param name="insertParameters">The parameter list to the insert command.</param>
/// <param name="updateParameters">The parameter list to the update command.</param>
/// <param name="deleteParameters">The parameter list to the delete command.</param>
public void UpdateData(DataTable table, string insertCommandText, string updateCommandText, string deleteCommandText, Dictionary<string, object> insertParameters, Dictionary<string, object> updateParameters, Dictionary<string, object> deleteParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateData(table, this._defaultCommandType, insertCommandText, updateCommandText, deleteCommandText, insertParameters, updateParameters, deleteParameters);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed data stored in a data table by specifying the command text and parameter to a series of commands - InsertCommand, UpdateCommand and DeleteCommand.
/// </summary>
/// <param name="table">The data table which stores the changed data.</param>
/// <param name="commandType">The command type of all of the commands</param>
/// <param name="insertCommandText">The command text for insert command.</param>
/// <param name="updateCommandText">The command text for update command.</param>
/// <param name="deleteCommandText">The command text for delete command</param>
/// <param name="insertParameters">The parameter list to the insert command.</param>
/// <param name="updateParameters">The parameter list to the update command.</param>
/// <param name="deleteParameters">The parameter list to the delete command.</param>
public void UpdateData(DataTable table, CommandType commandType, string insertCommandText, string updateCommandText, string deleteCommandText, Dictionary<string, object> insertParameters, Dictionary<string, object> updateParameters, Dictionary<string, object> deleteParameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand insertCommand = this.Connection.CreateCommand();
DbCommand updateCommand = this.Connection.CreateCommand();
DbCommand deleteCommand = this.Connection.CreateCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
insertCommand.CommandType = commandType;
updateCommand.CommandType = commandType;
deleteCommand.CommandType = commandType;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
insertCommand.CommandText = insertCommandText;
updateCommand.CommandText = updateCommandText;
deleteCommand.CommandText = deleteCommandText;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DbParameter parameter = this._dbProviderFactory.CreateParameter();
foreach (string parameterName in insertParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter.ParameterName = parameterName;
parameter.Value = insertParameters[parameterName];
insertCommand.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in updateParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter.ParameterName = parameterName;
parameter.Value = updateParameters[parameterName];
updateCommand.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string parameterName in deleteParameters.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parameter.ParameterName = parameterName;
parameter.Value = deleteParameters[parameterName];
deleteCommand.Parameters.Add(parameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateData(table, insertCommand, updateCommand, deleteCommand);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed data which is stored in table into database by specifying the thress commands: Inser commmand, update command and delete command.
/// </summary>
/// <param name="table">The table which stores the changed data.</param>
/// <param name="insertCommand">Insert command used for performing insertion operation.</param>
/// <param name="updateCommand">Update commmnad usede for performing modification operation.</param>
/// <param name="deleteCommand">Delete command used for performing deletion operation.</param>
public void UpdateData(DataTable table, DbCommand insertCommand, DbCommand updateCommand, DbCommand deleteCommand)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.DatabaseAdapter.Update(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed which is stored in a table or data row array into database using the command builder.
/// </summary>
/// <typeparam name="T">The type of changed data container: data table or data row array</typeparam>
/// <param name="instance">The data container holding the changed data.</param>
private void UpdateDataUsingCommandBuilder<T>(T instance)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataTable table = null;
DataRow[] dataRows = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
table = instance as DataTable;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataRows = instance as DataRow[];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DbCommandBuilder commandBuilder = this._dbProviderFactory.CreateCommandBuilder();
commandBuilder.DataAdapter = this.DatabaseAdapter;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the select command of the data adapter.
DbCommand selectComand = this._dbProviderFactory.CreateCommand();
selectComand.CommandText = string.Format("SELECT * FROM dbo.{0}", table.TableName);
selectComand.Connection = this.Connection;
this.DatabaseAdapter.SelectCommand = selectComand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Build the three commands of data adapter.
DbCommand insertCommand = commandBuilder.GetInsertCommand();
DbCommand updateCommand = commandBuilder.GetUpdateCommand();
DbCommand deleteCommand = commandBuilder.GetDeleteCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the database connection for the thress commands.
insertCommand.Connection = this.Connection;
updateCommand.Connection = this.Connection;
deleteCommand.Connection = this.Connection;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this._transaction != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
insertCommand.Transaction = this._transaction;
updateCommand.Transaction = this._transaction;
deleteCommand.Transaction = this._transaction;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(dataRows);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed which is stored in a table or data row array into database using the table-mapped stored procedure.
/// </summary>
/// <typeparam name="T">The type of changed data container: data table or data row array</typeparam>
/// <param name="instance">The data container holding the changed data.</param>
private void UpdateDataUsingMappedStoredProcedure<T>(T instance)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataTable table = null;
DataRow[] dataRows = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
table = instance as DataTable;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataRows = instance as DataRow[];
if (dataRows.Length == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Create the three commands of the database data adapter.
DbCommand insertCommand = this.Connection.CreateCommand();
DbCommand updateCommand = this.Connection.CreateCommand();
DbCommand deleteCommand = this.Connection.CreateCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the command type.
insertCommand.CommandType = CommandType.StoredProcedure;
updateCommand.CommandType = CommandType.StoredProcedure;
deleteCommand.CommandType = CommandType.StoredProcedure;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
insertCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string tableName = string.Empty;
if (table != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tableName = table.TableName;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tableName = dataRows[0].Table.TableName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the command text.
insertCommand.CommandText = this._storedProcedureNameMapping.GetInsertStoredProcedureName(tableName);
updateCommand.CommandText = this._storedProcedureNameMapping.GetModifyStoredProcedureName(tableName);
deleteCommand.CommandText = this._storedProcedureNameMapping.GetDeleteStoredProcedureName(tableName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this._transaction != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
insertCommand.Transaction = this._transaction;
updateCommand.Transaction = this._transaction;
deleteCommand.Transaction = this._transaction;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Discover the parameters of the three commands.
this.DiscoverParameters(insertCommand);
this.DiscoverParameters(updateCommand);
this.DiscoverParameters(deleteCommand);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the Source column and source version.
foreach (DbParameter parameter in insertCommand.Parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DbParameter parameter in updateCommand.Parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DbParameter parameter in deleteCommand.Parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Evaluate the commands for the database adapter.
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(dataRows);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed data which stored in a table which connect to another data table.
/// </summary>
/// <param name="table">The table containing the chaned data.</param>
/// <param name="rowState">The data row state which detemines which kinds of data will be saved.</param>
private void UpdateDependentTable(DataTable table, DataRowState rowState)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataViewRowState dataViewRowState = DataViewRowState.OriginalRows;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (rowState)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case DataRowState.Added:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataViewRowState = DataViewRowState.Added;
break;
}
case DataRowState.Deleted:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataViewRowState = DataViewRowState.Deleted;
break;
}
case DataRowState.Modified:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataViewRowState = DataViewRowState.ModifiedCurrent;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (table.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataTable parentTable = null;
DataTable childTable = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (rowState)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// DataRowState.Added|DataRowState.Modified: Parent table =>child table.
case DataRowState.Added:
case DataRowState.Modified:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataRelation relation in table.ParentRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentTable = relation.ParentTable;
if (parentTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(parentTable, rowState);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataRow[] updatedRows = table.Select(string.Empty, string.Empty, dataViewRowState);
this.UpdateIndependentTable(updatedRows);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow row in updatedRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
row.AcceptChanges();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataRelation relation in table.ChildRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
childTable = relation.ChildTable;
if (childTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(childTable, rowState);
}
break;
}
// DataRowState.Deleted: Child table => Parent table.
case DataRowState.Deleted:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Child Tables
foreach (DataRelation relation in table.ChildRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
childTable = relation.ChildTable;
if (childTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(childTable, rowState);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Itself
DataRow[] updatedRows = table.Select(string.Empty, string.Empty, dataViewRowState);
this.UpdateIndependentTable(updatedRows);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow row in updatedRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
row.AcceptChanges();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Parent Table.
foreach (DataRelation relation in table.ParentRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentTable = relation.ParentTable;
if (parentTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(parentTable, rowState);
}
}
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Save the changed data stored in an independent table.
/// </summary>
/// <param name="dataRows"></param>
private void UpdateIndependentTable(DataRow[] dataRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._useCommandBuilder)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDataUsingCommandBuilder<DataRow[]>(dataRows);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDataUsingMappedStoredProcedure<DataRow[]>(dataRows);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们来分析一下 public void UpdateData(DataSet dataInfo),这个方法对Data Access操作进行了高度的封装,Developer所做就是把更新过的Dataset传入UpdateData方法,其它的所有操作交给AppBlock来做。要实现这样的功能其实是很麻烦的,要考虑的因素很多:
-
需要把分析Dataset中DataTable之间的关系,确定先对那个Table 进行操作。
-
Dataset中的数据包含不同DataRowState的记录:Added,Modified,Deleted;需要和Dataset中DataTable之间的关系结合确定不同表,不同DataRowState数据的操作顺序。
-
使用Stored Procedure进行更新,需要考虑以下的Mapping:DataTable的Name和Stored Procedure Name;不同DataRowVersion的DataRow中的Field和Stored Procedure中的参数名。
我的解决方案是:
为了避免数据库中数据的冲突,我们数据更新的顺序是:Deleted Data->Modified Data->Added Data;考虑到表之间的主子关系,对于Added Data和Modified Data,我们应该先修改Parent Table,后修改Child,而对Deleted Data顺序却恰好相反。由于我们 不应该对DataSet中的Table的数量和关系做出任何假设,我们需要以一种递归的过程完成数据的更新。本着这样一个原则,我们来看我们的实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public void UpdateData(DataSet dataInfo)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//Deleted data.
if (dataInfo.GetChanges(DataRowState.Deleted) != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataTable table in dataInfo.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDependentTable(table, DataRowState.Deleted);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Modified Data.
if (dataInfo.GetChanges(DataRowState.Modified) != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataTable table in dataInfo.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDependentTable(table, DataRowState.Modified);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Added Data.
if (dataInfo.GetChanges(DataRowState.Added) != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataTable table in dataInfo.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDependentTable(table, DataRowState.Added);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
整个过程分3步骤,更新Deleted data.-> 更新Modified data.-> 更新Added data.真正的数据更新集中在UpdateDependentTable这样一个方法中。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void UpdateDependentTable(DataTable table, DataRowState rowState)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataViewRowState dataViewRowState = DataViewRowState.OriginalRows;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (rowState)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case DataRowState.Added:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataViewRowState = DataViewRowState.Added;
break;
}
case DataRowState.Deleted:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataViewRowState = DataViewRowState.Deleted;
break;
}
case DataRowState.Modified:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataViewRowState = DataViewRowState.ModifiedCurrent;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (table.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataTable parentTable = null;
DataTable childTable = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (rowState)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// DataRowState.Added|DataRowState.Modified: Parent table =>child table.
case DataRowState.Added:
case DataRowState.Modified:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DataRelation relation in table.ParentRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentTable = relation.ParentTable;
if (parentTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(parentTable, rowState);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataRow[] updatedRows = table.Select(string.Empty, string.Empty, dataViewRowState);
this.UpdateIndependentTable(updatedRows);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow row in updatedRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
row.AcceptChanges();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataRelation relation in table.ChildRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
childTable = relation.ChildTable;
if (childTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(childTable, rowState);
}
break;
}
// DataRowState.Deleted: Child table => Parent table.
case DataRowState.Deleted:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Child Tables
foreach (DataRelation relation in table.ChildRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
childTable = relation.ChildTable;
if (childTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(childTable, rowState);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Itself
DataRow[] updatedRows = table.Select(string.Empty, string.Empty, dataViewRowState);
this.UpdateIndependentTable(updatedRows);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow row in updatedRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
row.AcceptChanges();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Parent Table.
foreach (DataRelation relation in table.ParentRelations)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentTable = relation.ParentTable;
if (parentTable.GetChanges(rowState) == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDependentTable(parentTable, rowState);
}
}
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
通过传入的DataRowState创建一个DataViewRowState变量。对于DataRowState.Added和DataRowState.Modified,通过ParentRelations属性递归地获得并修改Parent Table的数据,然后跟新本Table,最后通过ChildRelations属性递归地获得并更新Child Table,如果发现对应的表已经更新,忽略并进入下一步。对于DataRowState.Deleted,则是一种反向的方法进行操作。而最终的Data Access 又落在了UpdateIndependentTable(updatedRows)方法上面。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void UpdateIndependentTable(DataRow[] dataRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (this._useCommandBuilder)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.UpdateDataUsingCommandBuilder<DataRow[]>(dataRows);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdateDataUsingMappedStoredProcedure<DataRow[]>(dataRows);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
通过._useCommandBuilder 属性判断是通过使用CommandBuilder生成Command还是通过Mapped Stored Procedure来生成Command更新数据。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void UpdateDataUsingCommandBuilder<T>(T instance)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable table = null;
DataRow[] dataRows = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
table = instance as DataTable;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataRows = instance as DataRow[];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DbCommandBuilder commandBuilder = this._dbProviderFactory.CreateCommandBuilder();
commandBuilder.DataAdapter = this.DatabaseAdapter;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the select command of the data adapter.
DbCommand selectComand = this._dbProviderFactory.CreateCommand();
selectComand.CommandText = string.Format("SELECT * FROM dbo.{0}", table.TableName);
selectComand.Connection = this.Connection;
this.DatabaseAdapter.SelectCommand = selectComand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Build the three commands of data adapter.
DbCommand insertCommand = commandBuilder.GetInsertCommand();
DbCommand updateCommand = commandBuilder.GetUpdateCommand();
DbCommand deleteCommand = commandBuilder.GetDeleteCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the database connection for the thress commands.
insertCommand.Connection = this.Connection;
updateCommand.Connection = this.Connection;
deleteCommand.Connection = this.Connection;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this._transaction != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
insertCommand.Transaction = this._transaction;
updateCommand.Transaction = this._transaction;
deleteCommand.Transaction = this._transaction;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(dataRows);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
上面的是一个泛型的方法,我们可以对一个单独的Table的一个DataRow数组进行的更新,代码相对还算清晰,相信对大部分人没有难度:首先照例使用DatabaseProviderFactory创建泛型的DbCommandBuilder,指定SelectCommand的CommandText(Select * From TableName),通过DbCommandBuilder创建3个Command传递给DatabaseAdapter的3 个Command属性。如果用户开始了一个Transaction,则把创建的Transaction映射到3个Command上。最后调用DatabaseAdapter.Update方法实现 数据的跟新。通过DbCommandBuilder是一种很简单的方法,但是存在很大的性能问题。造成性能降低的主要原因有两个:他是使用纯文本的SQL;为了避免数据库的并发操作引起的数据不一致,它在作数据更新的时候,会逐个字段地把Dataset原始数据和数据库作比较。所以我们一边采用stored procedure来更新数据库。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private void UpdateDataUsingMappedStoredProcedure<T>(T instance)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable table = null;
DataRow[] dataRows = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
table = instance as DataTable;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataRows = instance as DataRow[];
if (dataRows.Length == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Create the three commands of the database data adapter.
DbCommand insertCommand = this.Connection.CreateCommand();
DbCommand updateCommand = this.Connection.CreateCommand();
DbCommand deleteCommand = this.Connection.CreateCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the command type.
insertCommand.CommandType = CommandType.StoredProcedure;
updateCommand.CommandType = CommandType.StoredProcedure;
deleteCommand.CommandType = CommandType.StoredProcedure;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
insertCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string tableName = string.Empty;
if (table != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tableName = table.TableName;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tableName = dataRows[0].Table.TableName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the command text.
insertCommand.CommandText = this._storedProcedureNameMapping.GetInsertStoredProcedureName(tableName);
updateCommand.CommandText = this._storedProcedureNameMapping.GetModifyStoredProcedureName(tableName);
deleteCommand.CommandText = this._storedProcedureNameMapping.GetDeleteStoredProcedureName(tableName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this._transaction != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
insertCommand.Transaction = this._transaction;
updateCommand.Transaction = this._transaction;
deleteCommand.Transaction = this._transaction;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Discover the parameters of the three commands.
this.DiscoverParameters(insertCommand);
this.DiscoverParameters(updateCommand);
this.DiscoverParameters(deleteCommand);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Specify the Source column and source version.
foreach (DbParameter parameter in insertCommand.Parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DbParameter parameter in updateCommand.Parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DbParameter parameter in deleteCommand.Parameters)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Evaluate the commands for the database adapter.
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (instance is DataRow[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DatabaseAdapter.Update(dataRows);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
通过stored procedure的方式和上面通过Command Builder的步骤差不多。首先通过Conection创建3个Command,并指定Command type为CommandType.StoredProcedure。通过storedProcedureNameMapping根据Table name获得对应的stored procedure的名称,并赋值给3个Command的CommandText属性。如果开始了Transaction,与之关联。通过DiscoverParameters方法(这个一个Abstract方法,需要在具体的Database 类脂Override)给Command发现参数。接着我们为3个Command的parameter指定SourceColumn和SourceVersion,其中SourceColumn通过我们配置的dbParameterNameMapping来获得。SourceVersion通过方法GetSourceVersion(这个一个Abstract方法,需要在具体的Database 类脂Override;这个方法根据参数名称来无额定对应的Source Version:Original or Current)来获取。最后调用DatabaseAdapter.Update方法实现 数据的跟新。
5. SQLDatabase & OracleDatabase
由于ADO.NET 2.0提供的很多基于泛型编程的功能,使得我们把觉得大部分Data Access操作放在了上面这个Abstract Database中,所以SQLDatabase & OracleDatabase上的逻辑很少。我们只看SQLDatabase。上面我们提到Abstract Database提供两个Abstract方法需要在具体的Database中实现的:DiscoverParameters和GetSourceVersion。实现上SQLDatabase之包含这两个方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
namespace Artech.ApplicationBlock.DataAccess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Microsoft SQL Server Database
/// </summary>
public class SqlDatabase:Database
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public SqlDatabase()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Discover all of the parameters for the command realted stored procedure.
/// </summary>
/// <param name="command">Command</param>
public override void DiscoverParameters(DbCommand command)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (command.Connection.State != ConnectionState.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
command.Connection.Open();
}
SqlCommandBuilder.DeriveParameters(command as SqlCommand);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Get the DataRowVersion for the source column corresponding to the parameter based on the parameter name.
/// </summary>
/// <param name="parameterName"></param>
/// <returns></returns>
public override DataRowVersion GetSourceVersion(string parameterName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//p_abc_def=>DataRowVersion.Current
//o_abc_default=>DataRowVersion.Original
if(parameterName.StartsWith("@o"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DataRowVersion.Original;
}
return DataRowVersion.Current;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)