如何在两种方法中使用sql.trans?

时间:2011-06-02 05:22:23

标签: c# .net sql oracle

我剪切和粘贴按钮,同时剪切我有一个SQL操作和粘贴一个SQL操作。如何使用transation语句。

protected void btnCut_Click(object sender, EventArgs e)
{
        hidCutnode.Value = TreeView2.SelectedNode.Value;
        string sqlQuery = "update CUSTOMIZEDTREE set parentid='" + 0 + "' where nodeid='" + hidCutnode.Value + "'";
        string connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.69.200.62)(PORT=1521)))(CONNECT_DATA=(SID=orcl)));User Id=apex_demo;Password=apex_demo;";
        OracleConnection con = new OracleConnection(connectionString);
        con.Open();
        tran = con.BeginTransaction();
        OracleCommand cmd = new OracleCommand(sqlQuery, con);
        cmd.Transaction = tran;
        cmd.ExecuteNonQuery();
        con.Close();
}

protected void btnPaste_Click(object sender, EventArgs e)
{
        hidPastenode.Value = TreeView2.SelectedNode.Value;
        try
        {
            string sqlQuery = "update CUSTOMIZEDTREE set parentid='" + hidPastenode.Value + "' where nodeid='" + hidCutnode.Value + "'";
            string connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.69.200.62)(PORT=1521)))(CONNECT_DATA=(SID=orcl)));User Id=apex_demo;Password=apex_demo;";
            OracleConnection con = new OracleConnection(connectionString);
            con.Open();
            OracleCommand cmd = new OracleCommand(sqlQuery, con);
            tran = con.BeginTransaction();
           // cmd.Connection = con;
            cmd.Transaction = tran;
            cmd.ExecuteNonQuery();
            tran.Commit();

            con.Close();
            PopulateCustomTree();
        }
        catch (Exception ex)
        {
            tran.Rollback();
            ex.ToString();
        }
}

1 个答案:

答案 0 :(得分:1)

transaction对象作为函数参数传递给第二种方法,而不是{1}}在method1中。仅在第一种方法中使用commit