webservice标记为 [WebMethod(TransactionOption = TransactionOption.Required)]是否自动执行事务对此方法?
看了很多SAMPLE有两个方法实现事务,除了标记[WebMethod(TransactionOption = TransactionOption.Required)]
还有两件事:
1.[autocomplete]
2.方法指定事务的开始和异常处理等。
所以,请高手明示,是否[WebMethod(TransactionOption = TransactionOption.Required)]标识后则自动事务处理?

解决方案 »

  1.   

    我放一个 webservice 调用 存储过程的 demo 自己看看ProService.cs 公用类
    using System;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;using System.Data.SqlClient;  //引用空间
    using System.Data;
    /// <summary>
    /// ProService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProService : System.Web.Services.WebService {    public ProService () {        //如果使用设计的组件,请取消注释以下行 
            //InitializeComponent(); 
        }    [WebMethod]
        public bool ExcePro(string ConnectionString,string ProName,int ID)
        {
            SqlConnection Con = new SqlConnection(ConnectionString);
            Con.Open();
            SqlCommand Com = new SqlCommand(ProName, Con);//调用存储过程的方法
            Com.CommandType = CommandType.StoredProcedure;//存储过程名称
            try
            {
                SqlParameter p1 = new SqlParameter("@ID", SqlDbType.Int, 4);
                p1.Value = ID;
                Com.Parameters.Add(p1);
                Com.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                Con.Close();
                Con.Dispose();
            }
        }
    }Default.aspx.csusing System;
    using System.Data;
    using System.Configuration;
    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.Data.SqlClient;  //添加引用public partial class _Default : System.Web.UI.Page 
    {
        string cmdtxt ="Server=(local);DataBase=db_16;Uid=sa;Pwd=";    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string cmdtxt1 = "SELECT * FROM tb_04";
                SqlConnection Con = new SqlConnection(cmdtxt);
                Con.Open();
                SqlDataAdapter Da = new SqlDataAdapter(cmdtxt1, Con);
                DataSet ds = new DataSet();
                Da.Fill(ds);
                this.GridView1.DataSource = ds;
                this.GridView1.DataKeyNames = new string[] { "ID" };
                this.GridView1.DataBind();
            }
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = (int)this.GridView1.DataKeys[e.RowIndex].Value;
            DeProService.ProService pro = new DeProService.ProService();
            bool i = pro.ExcePro(cmdtxt, "delete_tb_04", ID);
            if (i == true)
            {
                Response.Write("<script>alert('数据删除成功!');location='Default.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('数据删除失败!');location='Default.aspx'</script>");
            }
        }
    }