如何开发一个学生成绩管理糸统(9)
? 這一節,我要說明的是在數據集中添加事務,
在這里說明一下事務的必要性:
大多數基于 web 的電子郵件客戶端都使用一個網格列出每條消息,除了包含郵件的信息(主題、發送者等等)外,還包括一個復選框。該界面允許用戶刪除多個消息,方法是先勾選上它們,再單擊“Delete Selected Messages ”按鈕。在用戶通常編輯多條不同記錄的情況下,一個批量編輯界面則是一項理想的選擇。用不著讓用戶對每條要修改的記錄,單擊Edit ,進行修改,然后單擊 Update ,批量編輯界面里每條記錄都有其各自的編輯界面。用戶可以快速地修改需要更改的那幾行,然后單擊“Update All” 按鈕保存這些更改。在本系列教程中,我們將探討如何創建插入、編輯和刪除批量數據的界面。
執行批操作時,重要的是確定是否會出現批操作中的某些操作可能成功而其它操作可能失敗的情況。考慮一個批量刪除界面:如果選擇的第一條記錄刪除成功,但第二條由于違反外健約束而失敗時,會出現什么情況?第一條記錄的刪除操作應該回滾?還是可以接受刪除第一條記錄?
如果想把批操作作為原子操作 對待,即所有的操作步驟要么都成功要么都失敗,則需要對數據訪問層進行擴展以支持數據庫事務 。數據庫事務確保INSERT 、UPDATE 和 DELETE 語句集的原子數在事務的保護下執行,大多數當代數據庫系統都支持數據庫事務特性。
?
事務概述
大多數數據庫都支持事務,它可以把多個數據庫命令當成一個邏輯單元進行處理。保證組成事務的數據庫命令是原子級表示所有的命令要么都失敗要么都成功。
一般來說,事務通過 SQL 語句來實現,使用如下的模式:
位于App_Code 文件夾的 DAL 子文件夾中。在 DAL 文件夾中創建一個名為 TransactionSupport 的子文件夾,并在里面添加一個名為s_courseinfoTableAdapter.TransactionSupport.cs 的新的類文件。該文件將保持s_courseinfoTableAdapter的部分實現,s_courseinfoTableAdapter 包含使用事務執行數據修改的方法。
在類中添加 如下代碼
注意 這里使用的類是partical 部分類,將數據庫事務操作封裝在類的內部
代碼 namespace GYsmsTableAdapters{
/// <summary>
///s_courseinfoTableAdapter 的摘要說明
/// </summary>
partial class s_courseinfoTableAdapter
{
private SqlTransaction _transaction;
private SqlTransaction Transaction
{
get
{
return this._transaction;
}
set
{
this._transaction = value;
}
}
}
在加入 開始事務的方法
?
代碼 public void BeginTransaction(){
// Open the connection, if needed
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
// Create the transaction and assign it to the Transaction property
this.Transaction = this.Connection.BeginTransaction();
// Attach the transaction to the Adapters
foreach (SqlCommand command in this.CommandCollection)
{
command.Transaction = this.Transaction;
}
this.Adapter.InsertCommand.Transaction = this.Transaction;
this.Adapter.UpdateCommand.Transaction = this.Transaction;
this.Adapter.DeleteCommand.Transaction = this.Transaction;
}
?
?
再下來是提交,和回滾事務操作
?
代碼 public void CommitTransaction(){
// Commit the transaction
this.Transaction.Commit();
// Close the connection
this.Connection.Close();
}
public void RollbackTransaction()
{
// Rollback the transaction
this.Transaction.Rollback();
// Close the connection
this.Connection.Close();
}
?
?
?
在類中添加事務更新方法
?
代碼 /// <summary>/// 事務性更新
/// </summary>
/// <param name="dataTable"></param>
/// <returns></returns>
public int UpdateWithTransaction(GYsms.s_courseinfoDataTable dataTable)
{
this.BeginTransaction();
try
{
// Perform the update on the DataTable
int returnValue = this.Adapter.Update(dataTable);
// If we reach here, no errors, so commit the transaction
this.CommitTransaction();
return returnValue;
}
catch
{
// If we reach here, there was an error, so rollback the transaction
this.RollbackTransaction();
throw;
}
}
?
?
這樣就完成了DAL層的事務添加了,
這樣,在BLL層添加事務性的方法,事務性添加,事務性刪除
?
?
代碼 /// <summary>/// 批量事務性更新
/// </summary>
/// <param name="products"></param>
/// <returns></returns>
public int UpdateWithTransaction(GYsms.s_courseDataTable Coursetable)
{
return Adapter.UpdateWithTransaction(Coursetable);
}
/// <summary>
/// 批量事務性刪除
/// </summary>
/// <param name="productIDs"></param>
public void DeleteCourseWithTransaction
(System.Collections.Generic.List<int> CourseIDs)
{
// Start the transaction
Adapter.BeginTransaction();
try
{
// Delete each product specified in the list
foreach (int CourseID in CourseIDs)
{
Adapter.Delete(CourseID);
}
// Commit the transaction
Adapter.CommitTransaction();
}
catch
{
// There was an error - rollback the transaction
Adapter.RollbackTransaction();
throw;
}
}
?
?
在WEB層的Course,aspx網頁上有一個批量刪除的方法,這樣寫就行
?
代碼 protected void DelSel_Click(object sender, EventArgs e){
List<int> delnums = new List<int>();
foreach (RepeaterItem Item in Repeater1.Items)
{
CheckBox ck = (CheckBox)Item.FindControl("ckbIndex");
Label lab = (Label)Item.FindControl("Labelid");
if (ck.Checked)
{
int j = Convert.ToInt32(lab.Text);
delnums.Add(j);
}
}
CourseAPI.DeleteCourseWithTransaction(delnums);
AlertMessage.Redirect(this, "刪除成功", "Course.aspx");
}
?
?
我在帖上在Repeater里面的代碼
?
?
?
代碼 <asp:Repeater ID="Repeater1" runat="server"><ItemTemplate>
<tr><td class="chk"><asp:CheckBox runat="server" ID="ckbIndex" /></td>
<td><asp:Label ID="Labelid" runat="server" Text='<%#Eval("id") %>'></asp:Label></td>
<td><asp:Label ID="Labelkid" runat="server" Text='<%#Eval("kid") %>'></asp:Label></td>
<td><asp:Label ID="Labelkname" runat="server" Text='<%#Eval("kname") %>'></asp:Label></td>
<td><asp:Label ID="Labelteacher" runat="server" Text='<%#Eval("teacher") %>'></asp:Label></td>
<td><asp:Label ID="Labelterm" runat="server" Text='<%#Eval("term") %>'></asp:Label></td>
<td><div><span><img src="images/edt.gif" alt="編輯" width="16" height="16" /><a href="Course_edit.aspx?action=edit&id=<%# Eval("id") %>" title="">編輯</a>
<img src="images/del2.gif" alt="刪除" width="16" height="16" /><a href="course.aspx?action=del&id=<%# Eval("id") %>" title="" onclick="return confirm('真的要刪除嗎?');" >刪除</a></span></div>
</ItemTemplate>
</asp:Repeater>
?
?
這樣就完成數據集事務的說明了,下一節,我會寫上Courseadd.aspx的頁面,批量添加課程信息.
?
轉載于:https://www.cnblogs.com/ScriptZhang/archive/2010/05/20/1739791.html
總結
以上是生活随笔為你收集整理的如何开发一个学生成绩管理糸统(9)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IHttpModule 与IHttpHa
- 下一篇: 6、struts.properties配