SqlTransaction 使用

SqlTransaction 使用

1. 在頁面上 拉 PlaceHolder、button、label


<form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    <asp:Button ID="btnSubmit" runat="server" Text="送出" onclick="btnSubmit_Click" />
    <br />
    <br />
    <asp:Label ID="lblDisplay" runat="server"></asp:Label>
</form>

 

2. 寫一個產生checkboxlist的函式

 


    /// <summary>
    /// 產生checkboxlist
    /// </summary>
    private void GenCheckboxlist()
    {
        CheckBoxList chl = new CheckBoxList();
        chl.ID = "chlTest";
        chl.Items.Add(new ListItem("學生", "student"));
        chl.Items.Add(new ListItem("上班族", "officer"));
        chl.Items.Add(new ListItem("老師", "teacher"));
        PlaceHolder1.Controls.Add(chl);
    }

 

3. 在page_load 時要產生 checkboxlist


protected void Page_Load(object sender, EventArgs e)
{
        GenCheckboxlist();
}

 

4. 重頭戲來了…寫入資料庫


    /// <summary>
    /// 寫入資料庫
    /// </summary>
    /// <param name="insertdata">要塞的值</param>
    private void SetData(IList<string> insertdata)
    {
        string conn = "Password=xxx;Persist Security Info=True;User ID=sa;Initial Catalog=MayTest;Data Source=.";
        try
        {
            using (SqlConnection cn = new SqlConnection(conn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = cn;
                    using (SqlTransaction tran = cn.BeginTransaction(IsolationLevel.ReadCommitted))
                    {
                        try
                        {
                            cmd.Transaction = tran;
                            cmd.CommandText = "insert into tbTest(utype) values(@utype)";
                            foreach (string ins in insertdata)
                            {
                                cmd.Parameters.Clear();
                                cmd.Parameters.AddWithValue("@utype", ins);
                                cmd.ExecuteNonQuery();
                            }
                            tran.Commit();
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            throw;
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

 

5. 送出 (我是來惡搞的那邊有興趣的話可以取消註解玩玩看 ^^)

 


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        IList<string> insertdata = new List<string>();

        // 取得哪些被勾選
        foreach (ListItem item in (PlaceHolder1.FindControl("chlTest") as CheckBoxList).Items)
        {
            if (item.Selected)
            {
                lblDisplay.Text += string.Format("您選中 {0}", item.Text) + "<br />";
                insertdata.Add(item.Text);
            }
        }

        //insertdata.Add("我是來惡搞的,我想要看看到底有沒有的進rollback,let me see see yoyoyoyoyoyoyoyoyoyyoyoyoyoyoyoyoyoyoyoyyoyoyoyoyoyoyoyoyoyoyyoyoyoyoyoyoyoyoyoyoyyoyoyoyoyoyoyoyoyoyoyyo");

        SetData(insertdata);
    }

 

參考:

Transaction.IsolationLevel 屬性

 

打完收工…呼~~~開懷大笑吐舌頭眨眼睛