【问题标题】:SQL Server stored procedure not seeing parameterSQL Server 存储过程看不到参数
【发布时间】:2018-12-11 05:27:09
【问题描述】:

我正在使用 C# 调用存储过程并从数据表中填充它,但我做错了。

我的存储过程:

CREATE PROCEDURE [dbo].[getStationInfo]
    @stationList AS dbo.udtableStationCode READONLY
AS  
BEGIN
    SELECT * 
    FROM stations
    WHERE StationCode IN (SELECT * FROM @stationList) 
END 

此过程使用此用户定义的表类型:

CREATE TYPE [dbo].[udtableStationCode] 
     AS TABLE (StationCode NVARCHAR(50))

我正在尝试将数据表发送到存储过程并将结果返回到另一个数据表中。这是我的 C#:

    using (SqlConnection con = new SqlConnection(strConn))
    {
        con.Open();

        using (SqlCommand cmd = new SqlCommand("getStationInfo", con))
        {
            using (SqlDataAdapter ada = new SqlDataAdapter(cmd))
            {
                using (DataTable dtStationsReturned = new DataTable())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Clear();
                    cmd.CommandText = "dbo.getStationInfo";
                    cmd.Parameters.AddWithValue("@stationList", dtStationCodes);
                    ada.Fill(dtStationsReturned);
                }
            }
        }
    }

无论我尝试什么,当我的代码到达“ada.Fill”行时,我都会收到错误:

过程“getStationInfo”没有名为“@stationList”的参数。

但是存储过程getStationInfo 显然有那个参数。谁能告诉我我做错了什么?任何帮助表示赞赏。

编辑:我检查了dtStationCodes的内容,没问题。

编辑:这是我创建dtStationCodes 数据表的方式:

DataTable dtStationCodes = new DataTable();
dtStationCodes.Columns.Add("StationCode", typeof(String));

【问题讨论】:

  • 您是否遇到与cmd.Parameters.Add("@stationList", SqlDbType.Structured).Value = dtStationCodes; 相同的错误? AddWithValue is evil.
  • @Dan Guzman,谢谢,尝试了你的建议,同样的错误。
  • 您有多个数据库吗?您是否更新了一个但没有更新另一个?
  • 糟糕!就是这样!我有多个连接,我的代码正在与不同的测试服务器通信。大家好心回答,很抱歉浪费了你们的时间。我是世界上最愚蠢的人。
  • @buckshot 这发生在我们最好的人身上,不断犯错并不断改进。放轻松

标签: c# sql-server stored-procedures parameter-passing user-defined-types


【解决方案1】:

试试这个:

using (SqlConnection con = new SqlConnection(strConn))
{
    con.Open();
    using (SqlCommand cmd = new SqlCommand("dbo.getStationInfo", con))
    {
        using (SqlDataAdapter ada = new SqlDataAdapter(cmd))
        {
            using (DataTable dtStationsReturned = new DataTable())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.CommandText = "dbo.getStationInfo";
                SqlParameter sp = new SqlParameter("@stationList", dtStationCodes);
                sp.SqlDbType = SqlDbType.Structured;
                sp.TypeName = "dbo.udtableStationCode";
                cmd.Parameters.Add(sp);
                ada.Fill(dtStationsReturned);
            }
        }
    }
}

有关传递用户定义表类型的另一个示例,请参阅this question

另请参阅this page,了解有关创建 SqlParameters 和将 TVP 传递给存储过程的更多信息。

【讨论】:

  • 谢谢@pcdev。我现在得到“必须仅为 UDT 参数设置 UdtTypeName 属性。”但你的建议似乎是一个UDT参数。类似的错误,同样的问题...?
  • 这给了我原来的错误。您修复了其他问题,但原始问题仍然存在。
  • 问题已解决,请参阅 cmets 到 OP。浪费大家时间,对不起。 : |
  • 嘿,这些事情发生了!很高兴您发现了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多