插入时从 SQL Server 获取 SCOPE_IDENTITY
我想已经太晚了,我太累了,看不到自己做错了什么。这是我正在尝试的:
int imageId = imageDal.AddImage(new SqlParameter[]
{
new SqlParameter("@IMAGE_ID",
SqlDbType.Int, Int32.MaxValue, ParameterDirection.Output,
true, 0, 0,"IMAGE_ID", DataRowVersion.Current,DBNull.Value),
new SqlParameter("@IMAGE",
SqlDbType.Image, 11, ParameterDirection.Input,
true, 0, 0,"IMAGE", DataRowVersion.Current,image)
});
public int AddImage(SqlParameter[] spParams)
{
SqlHelper.ExecuteNonQuery(BaseDAL.ConnectionStringImages, INSERT_IMAGE_SQL, spParams);
return Convert.ToInt32(spParams[0].Value);
}
存储过程:
[dbo].[sp_insert_image]
-- Add the parameters for the stored procedure here
@IMAGE_ID int OUT,
@IMAGE image
AS
BEGIN
INSERT INTO images
(IMAGE)
VALUES
(@IMAGE)
SELECT @IMAGE_ID = SCOPE_IDENTITY();
END
GO
我得到 DBNull 作为 spParams[0].Value。我尝试将 @IMAGE_ID 的值设置为我的存储过程中的常量,但它没有改变任何内容,因此问题不在于我的存储过程(这就是我的想法)。
当我从 sql management studio 执行该过程时,我看到 insert_id 返回。
I guess it is too late and I'm too tired to see what I'm doing wrong. Here is what I'm trying:
int imageId = imageDal.AddImage(new SqlParameter[]
{
new SqlParameter("@IMAGE_ID",
SqlDbType.Int, Int32.MaxValue, ParameterDirection.Output,
true, 0, 0,"IMAGE_ID", DataRowVersion.Current,DBNull.Value),
new SqlParameter("@IMAGE",
SqlDbType.Image, 11, ParameterDirection.Input,
true, 0, 0,"IMAGE", DataRowVersion.Current,image)
});
public int AddImage(SqlParameter[] spParams)
{
SqlHelper.ExecuteNonQuery(BaseDAL.ConnectionStringImages, INSERT_IMAGE_SQL, spParams);
return Convert.ToInt32(spParams[0].Value);
}
Stored Procedure:
[dbo].[sp_insert_image]
-- Add the parameters for the stored procedure here
@IMAGE_ID int OUT,
@IMAGE image
AS
BEGIN
INSERT INTO images
(IMAGE)
VALUES
(@IMAGE)
SELECT @IMAGE_ID = SCOPE_IDENTITY();
END
GO
I get DBNull as spParams[0].Value. I've tried setting value of @IMAGE_ID to a constant in my stored procedure yet it didn't change anything so the problem isn't with my stored procedure (that is what I think).
When I execute the procedure from sql management studio, I see the inserted_id returning..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终得到了executescalar 并直接从SP 返回
SCOPE_IDENTITY()
。如果有另一种方法可以做到这一点并从 sqlparameter 中获取值,我很想听听。
I ended up with executescalar and returning the
SCOPE_IDENTITY()
directly from SP.If there is another way of doing it and getting the value from the sqlparameter, I'd love to hear that out.
我想,问题出在 ExecuteNonQuery 方法中。它返回对象数组,而不是 sqlparam 的数组。
只需在输出 sqlparam 对象上有一个引用,然后从该引用中获取值即可。
祝你好运!
PS还有另一种解决方案。检查链接
http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-distributed-apps/160/Data-Access-Application-Block-Output-Parameters
I suppose, the problem is in ExecuteNonQuery method. It returns object array, instead of sqlparam's array.
Just have a ref on the output sqlparam object and get the value from that ref.
Good Luck!
P.S. There's another solution. Check the link
http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-distributed-apps/160/Data-Access-Application-Block-Output-Parameters
你这样放......
You put Like this....