taotaojue_01
2016-04-22 03:41 阅读 1.5k

SQL server 数据库@@Error变量值如何返回

其cmd的sql语句为:
private const string SQL_INSERT_ORDER = "Declare @ID int; Declare @ERR int;
INSERT INTO Orders VALUES(
@BillAddress1, @BillAddress2, @BillCity, @BillState, @BillZip, @BillCountry,(。。省略若干值。。) 'U');
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;
我目前需要读出@ERR自定义变量的值,网上提供的c# 代码如下:
var cmd = new sqlcommand(..);
cmd.executenonquery();--先执行sql语句。
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");
}--读取@err值
问题来了: 程序说没有有效的数据读入,应该怎么改?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐