关闭

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

494人阅读 评论(0) 收藏 举报

簡單的存儲過程如下:

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.可能有比這個更好的代碼,如果願意你可以發表你的評論,我很樂意接受.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63310次
    • 积分:977
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:17篇
    • 译文:0篇
    • 评论:13条
    文章分类