标签:style blog http io ar os 使用 sp for
create procedure [SqlAcTran] As
begin tran
declare @UserInfoError int
delete from [SqlAction] where username=‘测试‘
select @UserInfoError =@@error
if(@UserInfoError =0)
commit tran
else
rollback tran
go
using System;
using System.Data;
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.Data.SqlClient;
namespace WebApplication1

{
public partial class SqlAction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionStrings["DSN"].ConnectionString;
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "SqlAcTran";
com.ExecuteNonQuery();
con.Close();
}
}
}
using System;
using System.Data;
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.Data.SqlClient;
namespace WebApplication1

{
public partial class AdoAction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString=ConfigurationManager.ConnectionStrings["DSN"].ConnectionString;
con.Open();
//启动一个事务。
SqlTransaction myTran = con.BeginTransaction();
//为事务创建一个命令,注意我们执行双条命令,第一次执行当然成功。我们再执行一次,失败。
//第三次我们改其中一个命令,另一个不改,这时候事务会报错,这就是事务机制。
SqlCommand myCom = new SqlCommand();
myCom.Connection = con;
myCom.Transaction = myTran;
try
{
myCom.CommandText = "insert into SqlAction values (‘测试2‘,‘111‘)";
myCom.ExecuteNonQuery();
myCom.CommandText = "insert into SqlAction values (‘测试3‘,‘111‘)";
myCom.ExecuteNonQuery();
myTran.Commit();
Response.Write("成功执行");
}
catch (Exception Ex)
{
myTran.Rollback();
//创建并且返回异常的错误信息
Response.Write(Ex.ToString());
Response.Write("写入数据库失败");
}
finally
{
con.Close();
}
}

}
}
use myDatabase
go
create table ComAction
(
userID int identity,
userName varchar(30) not null,
userScore int not null
)
insert into ComAction values(‘张三‘,100)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.EnterpriseServices;
namespace prjEnterprise

{
[Transaction(TransactionOption.Required)]public class clsES:System.EnterpriseServices.ServicedComponent
{
public SqlConnection Conn;
public void dbAccess(int
pID1, int nScore)
{
try
{
SqlConnection Conn = new SqlConnection("user id=sa;password=;Initial Catalog=myDataBase;Data Source=.;");
Conn.Open();
SqlCommand sqlCommand = new SqlCommand("UPDATE ComAction SET userScore = " + nScore + " WHERE userID = " + pID1, Conn);
sqlCommand.ExecuteNonQuery();
ContextUtil.SetComplete();
Conn.Close();
}
catch (Exception e)
{
ContextUtil.SetAbort();
throw e;
}
finally
{
}
}
}
}C:\web
study\WebApplication1\WebApplication1\prjEnterprise\bin\Debug>regsvcs
prjEnterprise.dll
Microsoft(R) .NET Framework 服务安装实用工具版本
2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
警告: 程序集未声明 ApplicationAccessControl 属性。默认情况下启用应用程序安全性。
已安装的程序集:
程序集: C:\web
study\WebApplication1\WebApplication1\prjEnterprise\bin\D
ebug\prjEnterprise.dll
应用程序: prjEnterprise
TypeLib: C:\web
study\WebApplication1\WebApplication1\prjEnterprise\bin\
Debug\prjEnterprise.tlb
using System;
using System.Data;
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;
namespace WebApplication1

{
public partial class ComAcction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCommit1_Click(object sender, EventArgs e)
{
prjEnterprise.clsES myTest = new prjEnterprise.clsES();
try
{
myTest.dbAccess(1, 300);
Response.Write("事务成功!");
}
catch (Exception)
{
Response.Write("事务失败!");
}
}
protected void btnCommit2_Click(object sender, EventArgs e)
{
prjEnterprise.clsES myTest = new prjEnterprise.clsES();
try
{
myTest.dbAccess(1,1000000000);
Response.Write("事务成功!");
}
catch (Exception)
{
Response.Write("事务失败!");
}
}
}
}什么是异常处理
• 异常是正在执行的程序所遇到的任何错误情形或者意外行为。
•
很多原因都可以引起异常,例如,代码中错误、操作系统资源不可用、公共语言运行时(common language runtime)中的意外情况等等。
•
然而应用程序能够从上述的一些情况中恢复执行,但是大多数运行时异常是不可恢复的。在这种情况下,需要一种有效的方法来处理这些异常并给调用者提供相同的异常。
用结构化的异常处理方法来处理异常
•
在.NET Web服务中,对异常处理支持的关键点是由try...catch..finally语句提供的。
•
关键字try放在可能抛出异常的普通处理代码块之前。
• 关键字catch放在异常处理代码块之前。
•
关键字finally放在那些经常在异常处理后还需要执行的代码块之前。
•
一旦异常从try代码块中抛出,程序流切换到后面的第一个catch代码块
异常类
• Exception 所有异常对象的基类
•
SystemException 运行时产生的所有错误的基类
• IndexOutOfRangeException
当一个数组的下标超出范围时运行时引发
• NullReferenceException 当一个空对象被引用时运行时引发
•
InvalidOperationException 当对方法的调用对对象的当前状态无效时,由某些方法引发
• ArgumentException
所有参数异常的基类
• ArgumentNullException 在参数为空(不允许)的情况下,由方法引发
•
ArgumentOutOfRangeException 当参数不在一个给定范围之内时,由方法引发
• InteropException
目标在或发生在CLR外面环境中的异常的基类
• ComException 包含COM 类的HRESULT信息的异常
• SEHException
封装win32 结构异常处理信息的异常
优化异常
• 理解异常是一定会发生的
– 大多数的软件系统都不是百分之百可靠的!
–
要站在异常一定可能会发生的角度来编写异常
处理程序,应对程序有可能发生的错误。
– 建立一个良好的异常处理策略
• 处理未预料的异常
–
确保所有程序的入口都使用了try-catch
– 在catch中截获所有的异常
异常处理注意事项
•
当引发异常时,要提供有意义的文本。
• 要引发异常仅当条件是真正异常;也就是当一个正常的返回值不满足时。
•
如果你的方法或属性被传递一个坏参数,要引发一个ArgumentException异常。
•
当调用操作不适合对象的当前状态时,要引发一个InvalidOperationException异常。
• 要引发最适合的异常。
•
要使用链接异常,它们允许你跟踪异常树。
• 不要为正常或预期的错误使用异常。
• 不要为流程的正常控制使用异常。
•
不要在方法中引发NullReferenceException或IndexOutOfRangeException异常。
异常处理技术
•
记录异常
– 在文件中记录异常
– 在数据库中记录异常
– 在eventlog中记录异常
• 发送email通知异常
•
异常产生时,用友好(user-friendly)的方式通知用户
处理错误
• Page_Error事件
•
Application_Error事件
• 利用配置文件,自定义错误页面
– <customErrors
defaultRedirect="url" mo
de="RemoteOnly">
<error statusCode="code"
redirect="url
"></error>
</customErrors>
异常的例子我就不说了,大家知道的一定比我多,网上也可以搜到很多相关的例子来。
标签:style blog http io ar os 使用 sp for
原文地址:http://www.cnblogs.com/yzl495/p/4127164.html