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 server 2005)
创建一个虚拟表,该表以一种备用方式提供一个或多个表中的数据。CREATE VIEW 必须是查询批处理中的第一条语句。
Transact-SQL 语法约定
语法
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH <view_attribute> [ ,...n ] ]
AS s ......
----------Dbf 导入 Sql Server表----------
以下均以SQL2000、VFP6及以上的表为例
代码导入:查询分析器中执行如下语句(先选择对应的数据库)
-------------如果接受导入数据的SQL表已存在
--如果接受导入数据的SQL表已经存在
Insert Into 已经存在的SQL表名 Select * from openrowset('MSDASQL','Driver=Micros ......
CLR可以实现DML和DDL两种触发形式,但是本人一般不建议使用CLR的触发器,主要是考虑到效率问题。比如我们使用trigger来实现发mail等操作时,就要考虑pop3或是smtp等待时间,因为trigger本事就是个事务,也就是说,在smtp等待时间也算在了整个事务中,这样就会大大影响效率。
1.CLR DML触发器
DML指的是数据操作语言,也就 ......
bit:0或1的整型数字
int:从-2^31(-2,147,483,648)到2^31(2,147,483,647)的整型数字
smallint:从-2^15(-32,768)到2^15(32,767)的整型数字
tinyint:从0到255的整型数字
decimal:从-10^38到10^38-1的定精度与有效位数的数字
numeric:decimal的同义词
money:从-2^63(-922,337,203,685,477.580 ......
首先我很遗憾的告诉大家,因为微软的偷懒,目前UpdatePanel还不支持文件上传。变相的解决办法就是UpdatePanel中设置PostBackTrigger:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="serv ......