存储过程返回@@identity的问题!!!
有个表Orders,中有个主键OrderID,我想向表Orders插入一行,因为OrderID不能与以前的相同,而且不能为空,所以我就希望通过存储过程自己返回一个不相同的OrderID值。
----------------------------------
下面是我的存储过程:
CREATE PROCEDURE AddOrder
@OrderDate datetime
AS
insert Orders(OrderDate) values(@OrderDate)
return @@identity
GO
-----------------------------------
下面是我程序中的代码。
DataRow OrderRow=ds.Tables["Orders"].NewRow();
OrderRow["OrderID"]=0; //因为不能为空,所以设置为0
OrderRow["OrderDate"]=DateTime.Today;
ds.Tables["Orders"].Rows.Add(OrderRow);
SqlTransaction tsn=Cnn.BeginTransaction();
SqlCommand Cmd=new SqlCommand("AddOrder",Cnn,tsn);
Cmd.CommandType=CommandType.StoredProcedure;
SqlParameter param=Cmd.Parameters.Add("@OrderID",SqlDbType.Int);
param.SourceColumn="OrderID";
param.Direction=ParameterDirection.ReturnValue;
param=Cmd.Parameters.Add("@OrderDate",SqlDbType.DateTime);
param.SourceColumn="OrderDate";
param.SourceVersion=DataRowVersion.Current;
da.InsertCommand=Cmd;
da.Update(ds,"Orders"); //出错行。
-----------------------------------
但老是在最后一行出错,说OrderID字段不能为空,INSERT失败,
请问各位这是什么原因啊,
谢谢!!!