在 PetShop 4.0里面SqlServerDAL中的Order.cs有如下的代码:
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
// Read the returned @ERR
rdr.Read();
// If the error count is not zero throw an exception
if (rdr.GetInt32(1) != 0)
throw new ApplicationException("DATA INTEGRITY ERROR ON ORDER INSERT - ROLLBACK ISSUED");
}
其中cmd对象的Sql语句如下所示(我在调试中取出来的):
Declare @ID int;
Declare @ERR int;
INSERT INTO Orders VALUES
(@UserId, @Date, @ShipAddress1, @ShipAddress2, @ShipCity, @ShipState, @ShipZip, @ShipCountry, @BillAddress1, @BillAddress2,
@BillCity, @BillState, @BillZip, @BillCountry, 'UPS', @Total, @BillFirstName, @BillLastName, @ShipFirstName, @ShipLastName,
@AuthorizationNumber, 'US_en');
SELECT @ID=@@IDENTITY;
INSERT INTO OrderStatus VALUES(@ID, @ID, GetDate(), 'P');
SELECT @ERR=@@ERROR;
INSERT INTO LineItem VALUES( @ID, @LineNumber0, @ItemId0, @Quantity0, @Price0);
SELECT @ERR=@ERR+@@ERROR;
INSERT INTO LineItem VALUES( @ID, @LineNumber1, @ItemId1, @Quantity1, @Price1);
SELECT @ERR=@ERR+@@ERROR;SELECT @ID, @ERR
这么多语句用ExecuteReader()来执行,这能行吗?和事务处理有什么不同吗?
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
// Read the returned @ERR
rdr.Read();
// If the error count is not zero throw an exception
if (rdr.GetInt32(1) != 0)
throw new ApplicationException("DATA INTEGRITY ERROR ON ORDER INSERT - ROLLBACK ISSUED");
}
其中cmd对象的Sql语句如下所示(我在调试中取出来的):
Declare @ID int;
Declare @ERR int;
INSERT INTO Orders VALUES
(@UserId, @Date, @ShipAddress1, @ShipAddress2, @ShipCity, @ShipState, @ShipZip, @ShipCountry, @BillAddress1, @BillAddress2,
@BillCity, @BillState, @BillZip, @BillCountry, 'UPS', @Total, @BillFirstName, @BillLastName, @ShipFirstName, @ShipLastName,
@AuthorizationNumber, 'US_en');
SELECT @ID=@@IDENTITY;
INSERT INTO OrderStatus VALUES(@ID, @ID, GetDate(), 'P');
SELECT @ERR=@@ERROR;
INSERT INTO LineItem VALUES( @ID, @LineNumber0, @ItemId0, @Quantity0, @Price0);
SELECT @ERR=@ERR+@@ERROR;
INSERT INTO LineItem VALUES( @ID, @LineNumber1, @ItemId1, @Quantity1, @Price1);
SELECT @ERR=@ERR+@@ERROR;SELECT @ID, @ERR
这么多语句用ExecuteReader()来执行,这能行吗?和事务处理有什么不同吗?
再问下这些语句用ExecuteReader()在Access中也能用吗?
SELECT @ID=@@IDENTITY语句在Access中应该怎么写?
--------------------------------------------------Access一次只能执行一条SQL。
多条SQL需要多次执行,这是限制。
在SQL Server中,可以一次执行多条SQL语句。
Access使用的是Jet-SQL,SQL Server使用的是T-SQL,两者用法上相差很大。SELECT [@@identity]在Access中能用的
------------------------------------可以同时执行N条语句,象下面这样就可以了:cmd.CommandText = "INSERT INTO MyTable (N1,N2) VALUES (22,11)";
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT @@IDENTITY";
int n = (int)cmd.ExecuteScalar();
System.Console.WriteLine(n);
不过能连续执行就行
这样SELECT @@IDENTITY也就有效了,
这个还是蛮有用的^_^