易截截图软件、单文件、免安装、纯绿色、仅160KB

ASP.NET和SQL SERVER中使用事务示例

1,SqlServer存储过程的事务处理
一种比较通用的出错处理的模式大概如下:
Create procdure prInsertProducts
(
 @intProductId int,
 @chvProductName varchar(30),
 @intProductCount int
)
AS
Declare @intErrorCode int
Select @intErrorCode=@@Error
Begin transaction
 if @intErrorCode=0
   begin
     -insert products
     insert products(ProductID,ProductName,ProductCount)
     s(@intProductId,@chvProductName,@intProductCount)
     Select @intErrorCode=@@Error --每执行完一条t-sql语句马上进行检测,并把错误号保存到局部变量中
   end
 if @intErrorCode=0
   begin
     -update products
     update products set ProductName='MicroComputer' where ProductID=5
     Select @intErrorCode=@@Error
   end
if @intErrorCode=0
   commit transaction
else
   rollback transaction
 Return @intErrorCode --最好返回错误代号给调用的存储过程或应用程序
2,.Net中使用事务处理
SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
myConnection.Open();
SqlTransaction myTrans = myConnection***ginTransaction(); //使用New新生成一个事务
SqlCommand myCommand = new SqlCommand();
myCommand.Transaction = myTrans;
try
{
myCommand.CommandText = "Update Address set location='23 rain street' where userid='0001'";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Record is udated.");
}
catch(Exception e)
{
myTrans.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Sorry, Record can not be updated.");
}
finally
{
myConnection.Close();
}


相关文档:

修改SQL数据库的名称


其实在修改数据库名称之前,如果有用户连接到数据库的话会造成数据库重命名失败,可以先执行
select spid
from master.dbo.sysprocesses
where dbid=db_id('OldDbName')
结果集中显示的是当前连接到数据库OldDbName的连接
比如结果是
79
81
当然,实际值应该不是这两个
然后执行
kill 79
kill 81
关闭已建立 ......

SQL优化过程中常见Oracle HINT的用法

 在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法:
1. /*+ALL_ROWS*/
表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化.
例如:
SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_IN from BSEMPMS WHERE EMP_NO='SCOTT';
2. /*+FIRST_ROWS*/
表 ......

java与SQL数据库桥接方式易错问题与设计核心

 文章原创,转载请与Blog主人联系,robin9257@hotmail.com
JAVA与SQL桥接的优缺点:
1、优点:不用下载驱动程序,允许用相同代码访问不同DBMS。
2、缺点:效率低。
经常出现的报错点:
1、java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
  
&nbs ......

SQL Server CLR全功略之四

CLR可以实现DML和DDL两种触发形式,但是本人一般不建议使用CLR的触发器,主要是考虑到效率问题。比如我们使用trigger来实现发mail等操作时,就要考虑pop3或是smtp等待时间,因为trigger本事就是个事务,也就是说,在smtp等待时间也算在了整个事务中,这样就会大大影响效率。
1.CLR DML触发器
DML指的是数据操作语言,也就 ......

优秀ASP.NET程序员的修炼之路


<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体" ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号