【问题标题】:ADO.NET - Trouble Getting Output ParameterADO.NET - 获取输出参数时遇到问题
【发布时间】:2013-11-22 22:35:50
【问题描述】:

我的 DBA 创建了以下存储过程,他坚持认为在 SQL Server 中调用时可以正常工作:

CREATE procedure [dbo].[GetParentID]
    @SSHIP_AppID as varchar(50),
    @ParentID as varchar(150) OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
    SELECT @ParentID = a.iBuild_GUID

    FROM dbo.XRef_iBuild_SSHIP as a
    WHERE a.SSHIP_appId = @SSHIP_AppID
    AND a.SSHIP_appId <> ''
END

我创建了以下 ADO.NET Wrapper,但在获取输出参数时遇到了问题。我不断取回“OUTPUT”作为它的值:

 private string GetParentId(string appId)
        {
            var connection = new SqlConnection();
            string parentId = String.Empty;
            try
            {
                connection.ConnectionString = "...)
                var command = new SqlCommand("GetParentId", connection);

                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@SSHIP_AppID", appId));

                command.Parameters.Add(new SqlParameter("@ParentID", ParameterDirection.Output));

                connection.Open();

                command.ExecuteNonQuery();

                parentId = (command.Parameters["@ParentId"].Value).ToString();
            }
            catch (Exception ex)
            {
                LogError(appId, ex.ToString(), "Interface12 - Cannot get ParentId", null, 0);
            }
            finally
            {
                connection.Close();
            }
            return parentId;
        }
    }

我做错了什么?

【问题讨论】:

  • 一个错误是 command.Parameters[@ParentId"] 应该是 ]"@ParentID"] 但我仍然得到同样的错误

标签: sql-server ado.net


【解决方案1】:

new SqlParameter("@ParentID", ParameterDirection.Output) 中,第二个参数被视为object value 参数并显然转换为字符串。

(在我看来,这种隐式转换是 ADO.NET 中的一个设计缺陷。它应该为任何未知的输入类型抛出异常。)。

选择更好的重载。

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    相关资源
    最近更新 更多