代码之家  ›  专栏  ›  技术社区  ›  buckshot

SQL Server存储过程未看到参数

  •  0
  • buckshot  · 技术社区  · 7 年前

    我使用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))
    

        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);
                    }
                }
            }
        }
    

    无论我尝试什么,当我的代码到达阿达,加油'行,我得到错误:

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

    getStationInfo 显然有这个参数。有人能告诉我我做错了什么吗?谢谢你的帮助。

    编辑:我已经检查了 dtStationCodes

    编辑:下面是我如何创建我的 数据表:

    DataTable dtStationCodes = new DataTable();
    dtStationCodes.Columns.Add("StationCode", typeof(String));
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   pcdev    7 年前

    试试这个:

    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