linxi27的专栏

好好學習dotnet

dotnet中调用存储过程的用法,包含事务处理

簡單的存儲過程如下:

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;



public partial class TaskAdministration_test : System.Web.UI.Page
...{
    
protected void Page_Load(object sender, EventArgs e)
    
...{
       

    }


    
protected void Button1_Click(object sender, EventArgs e)
    
...
        ExcelOperation OEM 
= new ExcelOperation();
        
string isCommit = "Y";
        Database db 
= DatabaseFactory.CreateDatabase();
        DbConnection tconn 
= db.CreateConnection();
        DbCommand cmd 
= tconn.CreateCommand();
        DbCommand cmd1 
= tconn.CreateCommand();
       tconn.Open();
       DbTransaction ttran
= tconn.BeginTransaction();
       cmd.Connection 
= ttran.Connection;
       cmd1.Connection 
= ttran.Connection;
       cmd.Transaction 
= ttran;
       cmd1.Transaction 
= ttran;
       
try
       
...{
           cmd.CommandText 
= "PR_INSERTDM";
           cmd.CommandType 
= CommandType.StoredProcedure;
            OEM.AddInParameter(cmd, 
"ptaskid",txt1.Text);
            OEM.AddInParameter(cmd, 
"proleid",  txt2.Text);
            OEM.AddInParameter(cmd, 
"pmembername", txt3.Text);
            cmd.ExecuteNonQuery();
         
            cmd1.CommandText 
= "PR_INSERTDT";
            cmd1.CommandType 
= CommandType.StoredProcedure;
            OEM.AddInParameter(cmd1, 
"PTASKID", txt4.Text);
            OEM.AddInParameter(cmd1, 
"PROLEID", txt5.Text);
            OEM.AddInParameter(cmd1, 
"PDEPTNAME", txt6.Text);
            cmd1.ExecuteNonQuery();
       }

       
catch(Exception ex)
       
...{
           isCommit 
= "No";
           tconn.Close();
       }

       
finally
       
...{
           
if (isCommit == "No")
           
...{
               ttran.Rollback();
               
throw new Exception("操作失敗!");
           }

           
else
           
...{
               ttran.Commit();
           }

           tconn.Close();
       }
      
    }

}

説明:1.存儲過程和類都比較簡單,目的是盡量簡單的説明在事務中調用多個存儲過程的用法.

2.可能有比這個更好的代碼,如果願意你可以發表你的評論,我很樂意接受.

阅读更多
想对作者说点什么? 我来说一句

存储过程 与 事务管理

高级SQL

laoj1228 laoj1228

2016-08-03 00:08:51

阅读数:1194

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭