存储过程:
ALTER PROCEDURE dbo.myTran
(
@title varchar(50),
@body varchar(500)
)
AS
declare @id int
begin Transaction
insert into guestbook(title,body) values(@title,@body)
set @id=@@identity
insert into guestbook(title,body) values(@id,@body)
if @@error<>0--发生错误
begin
rollback transaction
return 0--执行失败
end
else
begin
commit transaction
return 1--执行成功
end
RETURN
(
@title varchar(50),
@body varchar(500)
)
AS
declare @id int
begin Transaction
insert into guestbook(title,body) values(@title,@body)
set @id=@@identity
insert into guestbook(title,body) values(@id,@body)
if @@error<>0--发生错误
begin
rollback transaction
return 0--执行失败
end
else
begin
commit transaction
return 1--执行成功
end
RETURN
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
标题:<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<br />
内容:<asp:TextBox ID="txtBody" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="提交并执行事务" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
后台代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
标题:<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<br />
内容:<asp:TextBox ID="txtBody" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="提交并执行事务" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
using 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
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection con = new SqlConnection(connstr);
if (con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
string procName = "dbo.myTran";
SqlCommand cmd = new SqlCommand(procName,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@title",SqlDbType.VarChar,50);
cmd.Parameters.Add("@body",SqlDbType.VarChar,500);
cmd.Parameters[0].Value = this.txtTitle.Text.Trim();
cmd.Parameters[1].Value = this.txtBody.Text.Trim();
int intResult=Convert.ToInt32(cmd.ExecuteNonQuery());
if (intResult > 0)
{
Response.Write("事务执行成功");
}
else
{
Response.Write("事务执行失败");
}
}
}
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
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection con = new SqlConnection(connstr);
if (con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
string procName = "dbo.myTran";
SqlCommand cmd = new SqlCommand(procName,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@title",SqlDbType.VarChar,50);
cmd.Parameters.Add("@body",SqlDbType.VarChar,500);
cmd.Parameters[0].Value = this.txtTitle.Text.Trim();
cmd.Parameters[1].Value = this.txtBody.Text.Trim();
int intResult=Convert.ToInt32(cmd.ExecuteNonQuery());
if (intResult > 0)
{
Response.Write("事务执行成功");
}
else
{
Response.Write("事务执行失败");
}
}
}