ORM: 开发自己的Data Access Application Block - Part IIII
2008-12-06 10:15:37 来源:WEB开发网上面的是一个泛型的方法,我们可以对一个单独的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来更新数据库。
private void UpdateDataUsingMappedStoredProcedure<T>(T instance)
{
DataTable table = null;
DataRow[] dataRows = null;
if (instance is DataTable)
{
table = instance as DataTable;
}
if (instance is DataRow[])
{
dataRows = instance as DataRow[];
if (dataRows.Length == 0)
{
return;
}
}
//Create the three commands of the database data adapter.
DbCommand insertCommand = this.Connection.CreateCommand();
DbCommand updateCommand = this.Connection.CreateCommand();
DbCommand deleteCommand = this.Connection.CreateCommand();
//Specify the command type.
insertCommand.CommandType = CommandType.StoredProcedure;
updateCommand.CommandType = CommandType.StoredProcedure;
deleteCommand.CommandType = CommandType.StoredProcedure;
insertCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
string tableName = string.Empty;
if (table != null)
{
tableName = table.TableName;
}
else
{
tableName = dataRows[0].Table.TableName;
}
//Specify the command text.
insertCommand.CommandText = this._storedProcedureNameMapping.GetInsertStoredProcedureName(tableName);
updateCommand.CommandText = this._storedProcedureNameMapping.GetModifyStoredProcedureName(tableName);
deleteCommand.CommandText = this._storedProcedureNameMapping.GetDeleteStoredProcedureName(tableName);
if (this._transaction != null)
{
insertCommand.Transaction = this._transaction;
updateCommand.Transaction = this._transaction;
deleteCommand.Transaction = this._transaction;
}
//Discover the parameters of the three commands.
this.DiscoverParameters(insertCommand);
this.DiscoverParameters(updateCommand);
this.DiscoverParameters(deleteCommand);
//Specify the Source column and source version.
foreach (DbParameter parameter in insertCommand.Parameters)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
foreach (DbParameter parameter in updateCommand.Parameters)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
foreach (DbParameter parameter in deleteCommand.Parameters)
{
if (parameter.Direction == ParameterDirection.ReturnValue)
{
continue;
}
parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
}
//Evaluate the commands for the database adapter.
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
if (instance is DataTable)
{
this.DatabaseAdapter.Update(table);
}
if (instance is DataRow[])
{
this.DatabaseAdapter.Update(dataRows);
}
}
- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››自己动手写iPhone wap浏览器之界面架构篇
- ››自己也能DIY个性真人QQ表情
- ››自己动手!巧法让酷狗动感歌词更完美
- ››自己编译Google Android内核的Linux源码
- ››自己写的一个jquery模板引擎(json比较好用)
- ››开发一个自己的HTML在线编辑器(一)
- ››开发一个自己的HTML在线编辑器(二)
更多精彩
赞助商链接