ORM: 开发自己的Data Access Application Block - Part IIII
2008-12-06 10:15:37 来源:WEB开发网通过传入的DataRowState创建一个DataViewRowState变量。对于DataRowState.Added和DataRowState.Modified,通过ParentRelations属性递归地获得并修改Parent Table的数据,然后跟新本Table,最后通过ChildRelations属性递归地获得并更新Child Table,如果发现对应的表已经更新,忽略并进入下一步。对于DataRowState.Deleted,则是一种反向的方法进行操作。而最终的Data Access 又落在了UpdateIndependentTable(updatedRows)方法上面。
private void UpdateIndependentTable(DataRow[] dataRows)
{
if (this._useCommandBuilder)
{
this.UpdateDataUsingCommandBuilder<DataRow[]>(dataRows);
}
else
{
this.UpdateDataUsingMappedStoredProcedure<DataRow[]>(dataRows);
}
}
通过._useCommandBuilder 属性判断是通过使用CommandBuilder生成Command还是通过Mapped Stored Procedure来生成Command更新数据。
private void UpdateDataUsingCommandBuilder<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[];
}
DbCommandBuilder commandBuilder = this._dbProviderFactory.CreateCommandBuilder();
commandBuilder.DataAdapter = this.DatabaseAdapter;
//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;
//Build the three commands of data adapter.
DbCommand insertCommand = commandBuilder.GetInsertCommand();
DbCommand updateCommand = commandBuilder.GetUpdateCommand();
DbCommand deleteCommand = commandBuilder.GetDeleteCommand();
this.DatabaseAdapter.InsertCommand = insertCommand;
this.DatabaseAdapter.UpdateCommand = updateCommand;
this.DatabaseAdapter.DeleteCommand = deleteCommand;
//Specify the database connection for the thress commands.
insertCommand.Connection = this.Connection;
updateCommand.Connection = this.Connection;
deleteCommand.Connection = this.Connection;
if (this._transaction != null)
{
insertCommand.Transaction = this._transaction;
updateCommand.Transaction = this._transaction;
deleteCommand.Transaction = this._transaction;
}
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在线编辑器(二)
更多精彩
赞助商链接