1.CREATE PROCEDURE AddOrder
(
@OrderID INT OUT,
@CustomerID NCHAR(5),
@OrderDate DATETIME )
AS
INSERT INTO Orders
(CustomerID ,
OrderDate
)
VALUES
( @CustomerID ,
@OrderDate
)
--SELECT @OrderID = @@IDENTITY //使用触发器有可能出现问题
SET @OrderID = SCOPE_IDENTITY() //返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值。
GO
OrderID自动增长值的获取通过输出参数来完成,这个相当不错,如果使用SqlDataAdapter.RowUpdated事件来处理那效率会很低。
对insertOrderComm对象的定义为:
SqlCommand insertOrderComm = new SqlCommand("AddOrder",_conn);
insertOrderComm.CommandType = CommandType.StoredProcedure;
insertOrderComm.Parameters.Add("@OrderID",SqlDbType.Int,4,"OrderID");
insertOrderComm.Parameters["@OrderID"].Direction = ParameterDirection.Output;
insertOrderComm.Parameters.Add("@OrderDate",SqlDbType.DateTime,8,"OrderDate");
insertOrderComm.Parameters.Add("@CustomerID",SqlDbType.NChar,5,"CustomerID");
2、
SET @DID
=@@IDENTITY
return 1
运行的很好。
可是换成:
SET @DID =SCOPE_IDENTITY()
总是 System.DBNull。
returnObject=db.ReturnValue(ProcedureName,20,sqlpara);
db.Excute(ProcedureName,sqlpara);
returnObject= sqlpara[20].Value;
}
catch (System.Exception e)
{
throw e;
}
finally
{
db.Close();
}
if(returnObject!=null&&returnObject!=System.DBNull.Value)
sn=System.Convert.ToInt32(returnObject);
转载于:https://www.cnblogs.com/flashicp/archive/2007/05/23/757064.html