输出参数值错误

发布于 2024-11-16 20:57:30 字数 2045 浏览 2 评论 0原文

我将一个值传递给存储过程中的参数,并将其方向声明为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-11-23 20:57:30

这很可能是因为您没有指定参数的大小。尝试更改代码以包含参数的大小,如下所示:

cmd.Parameters.Add("@FileName", SqlDbType.NVarChar, 100).Value = ImageFileNameNoExt

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:

cmd.Parameters.Add("@FileName", SqlDbType.NVarChar, 100).Value = ImageFileNameNoExt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文