输出参数值错误
我将一个值传递给存储过程中的参数,并将其方向声明为 ParameterDirection.InputOutput。在存储过程中,该参数也被声明为 OUTPUT 参数,并且从存储过程返回的值是正确的。我知道这一点是因为我已经测试过单独执行存储过程并使用 PRINT
在整个过程中显示不同的值。此外,存储过程的最后部分使用我期望返回并且按预期显示的值将记录插入数据库。但是,当我在执行 SqlCommand 后尝试从参数中检索值时,该值仍然是我传递给存储过程的先前值。我以前也这样做过,而且我确信我的做法没有什么不同。这是我的命令的一部分:
Dim cmd As New SqlCommand("StoredProcedure", Conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@FileName", SqlDbType.NVarChar).Value = ImageFileNameNoExt
cmd.Parameters("@FileName").Direction = ParameterDirection.InputOutput
cmd.ExecuteNonQuery()
ImageFileName = cmd.Parameters("@FileName").Value
这里,我期望 ImageFileName
是从存储过程返回的 @FileName
参数的值。但是,它仍然与首先传递给过程的 ImageFileNameNoExt
值相同。
有人可以告诉我我做错了什么吗?谢谢。
编辑
ALTER PROCEDURE [dbo].[sp_ContentPages_Images_Update]
@PageID int
,@FileName nvarchar(100) OUTPUT
,@Ext nvarchar(100)
AS
BEGIN
SET NOCOUNT ON;
--As the FileName is a unique key, ensure that the value being entered into the database will be unique.
--If its not, then we can generate a new one
SET @FileName = REPLACE(@FileName, ' ', '-')
DECLARE @i int
,@FileNameCheck nvarchar(200)
SET @i = 2
SET @FileNameCheck = @FileName + @Ext
WHILE (SELECT COUNT(*)
FROM [ContentPages_Images]
WHERE [FileName]=@FileNameCheck
AND (@PageID IS NULL OR NOT @PageID=ID)
) > 0
BEGIN
SET @FileNameCheck = @FileName + '-' + CAST(@i as nvarchar(3)) + @Ext
SET @i = @i + 1
END
SET @FileName = @FileNameCheck
--Get new layout value
DECLARE @Layout int
SET @Layout = 1
IF (SELECT COUNT(*) FROM [ContentPages_Images] WHERE PageID=@PageID) > 0
BEGIN
SET @Layout = (SELECT MAX(Layout) FROM [ContentPages_Images] WHERE PageID=@PageID) + 1
END
INSERT INTO [ContentPages_Images]
(PageID, [FileName], [Layout])
VALUES
(@PageID, @FileName, @Layout)
END
I am passing a value to a parameter in a Stored Procedure and also declaring it's direction as ParameterDirection.InputOutput
. In the Stored Procedure, the parameter is also declared as an OUTPUT parameter and the value being returned from the Stored Procedure is correct. I know this because I have tested executing the Stored Procedure on its own and using PRINT
to display different values throughout the procedure. Also, the final part of the Stored Procedure inserts a record into the database using the value that I am expecting to be returned and that is appearing as expected. However, when I attempt to retrieve the value from the parameter after the SqlCommand
has been executed, the value is still the previous value that I passed to the Stored Procedure. I have done this before and I am sure I've done it no differently. Here's a part of my command:
Dim cmd As New SqlCommand("StoredProcedure", Conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@FileName", SqlDbType.NVarChar).Value = ImageFileNameNoExt
cmd.Parameters("@FileName").Direction = ParameterDirection.InputOutput
cmd.ExecuteNonQuery()
ImageFileName = cmd.Parameters("@FileName").Value
Here, I am expecting ImageFileName
to be the value of the @FileName
parameter, returned from the Stored Procedure. However, it's still the same value as ImageFileNameNoExt
which was passed to the procedure in the first place.
Can someone please tell me what I'm doing wrong? Thanks.
EDIT
ALTER PROCEDURE [dbo].[sp_ContentPages_Images_Update]
@PageID int
,@FileName nvarchar(100) OUTPUT
,@Ext nvarchar(100)
AS
BEGIN
SET NOCOUNT ON;
--As the FileName is a unique key, ensure that the value being entered into the database will be unique.
--If its not, then we can generate a new one
SET @FileName = REPLACE(@FileName, ' ', '-')
DECLARE @i int
,@FileNameCheck nvarchar(200)
SET @i = 2
SET @FileNameCheck = @FileName + @Ext
WHILE (SELECT COUNT(*)
FROM [ContentPages_Images]
WHERE [FileName]=@FileNameCheck
AND (@PageID IS NULL OR NOT @PageID=ID)
) > 0
BEGIN
SET @FileNameCheck = @FileName + '-' + CAST(@i as nvarchar(3)) + @Ext
SET @i = @i + 1
END
SET @FileName = @FileNameCheck
--Get new layout value
DECLARE @Layout int
SET @Layout = 1
IF (SELECT COUNT(*) FROM [ContentPages_Images] WHERE PageID=@PageID) > 0
BEGIN
SET @Layout = (SELECT MAX(Layout) FROM [ContentPages_Images] WHERE PageID=@PageID) + 1
END
INSERT INTO [ContentPages_Images]
(PageID, [FileName], [Layout])
VALUES
(@PageID, @FileName, @Layout)
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是因为您没有指定参数的大小。尝试更改代码以包含参数的大小,如下所示:
This is most likely because you haven't specified a size for the parameter. Try changing your code to include the parameter's size like the following: