首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当试图在C#中传递2个参数时,Server存储过程或函数指定的参数太多

当试图在C#中传递2个参数时,Server存储过程或函数指定的参数太多
EN

Stack Overflow用户
提问于 2020-10-06 15:22:05
回答 1查看 136关注 0票数 0

我正在尝试执行一个名为getLastFeatureUpdate的存储过程。

我将在下面逐一解释这个问题:

我在SQL中创建了如下表:

代码语言:javascript
运行
复制
CREATE TABLE testTable
(
      DayTime INT     NOT NULL, /*yyddhhmm, 1010102345*/
      FeatureNbr      SMALLINT  NOT NULL,
      Val FLOAT(53)   NOT NULL
);

我现在已经创建了一个名为getLastFeatureUpdate的存储过程。这里需要注意的是,我使用了两个参数@maxDateTime@tableName,因为它们每次都是不同的。然后,我将在后面的C#代码中传递这两个参数。

存储过程(如果我从过程和C#代码中删除C#)。那么,代码确实起作用)

代码语言:javascript
运行
复制
CREATE PROCEDURE getLastFeatureUpdate
    @maxDateTime float(53) = 0,
    @tableName text
AS
    SELECT
        test.FeatureNbr,
        test.DayTime,
        test.Val
    FROM
        @tableName test 
    WHERE
        DayTime = (SELECT MAX(DayTime)
                   FROM @tableName
                   WHERE FeatureNbr = test.FeatureNbr --This is what you are missing
                     AND DayTime <= @maxDateTime)   --10102248

我想从C#返回数据的testTable代码。如:MessageBox.Show(d1 + "," + d2 + "," + d3);所示

但这是我得到错误的地方:

过程或函数getLastFeatureUpdate指定的参数太多

注意,如果我没有在这里传递@tableName的第2行,代码就会正常工作(然后我必须删除@tableName作为存储过程getLastFeatureUpdate中的参数)。

代码语言:javascript
运行
复制
cmd.Parameters.Add(new SqlParameter("@maxDateTime", 10102248));
cmd.Parameters.Add(new SqlParameter("@tableName", "testTable")); //If not using this parameter, the code will work

C#代码:

代码语言:javascript
运行
复制
void getLastFeatureUpdate()
{
        using (SqlConnection conn = new SqlConnection(GetConnectionString()))
        {
            conn.Open();

            // 1. create a command object identifying the stored procedure
            SqlCommand cmd = new SqlCommand("getLastFeatureUpdate", conn);

            // 2. set the command object so it knows to execute a stored procedure
            cmd.CommandType = CommandType.StoredProcedure;

            // 3. add parameter to command, which will be passed to the stored procedure
            cmd.Parameters.Add(new SqlParameter("@maxDateTime", 10102248));
            cmd.Parameters.Add(new SqlParameter("@tableName", "testTable")); //If not using this parameter, the code will work

            // execute the command
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                // iterate through results, printing each to console
                while (rdr.Read())
                {
                    int v1 = (int)rdr["DayTime"];
                    int v2 = (Int16)rdr["FeatureNbr"];
                    double v3 = (double)rdr["Val"];

                    MessageBox.Show(v1 + "," + v2 + "," + v3);
                }
            }
        }
}

static private string GetConnectionString()
{
    return "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\andre\\source\\repos\\TestDatabaseCreation\\DatabaseTest.mdf;Integrated Security=True;Connect Timeout=30";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-06 15:25:55

不能参数化Server中的表名,因此: SQL无效,而且CREATE PROC实际上没有运行。旧proc的内容是什么:只有您可以知道,但是:它不是显示的代码。它可能是一个虚拟版本,您在某一阶段的开发。试着打字:

代码语言:javascript
运行
复制
exec sp_helptext getLastFeatureUpdate;

具体来说,服务器应该告诉您:

代码语言:javascript
运行
复制
Msg 1087, Level 16, State 1, Procedure getLastFeatureUpdate, Line 12 [Batch Start Line 0]
Must declare the table variable "@tableName".
Msg 1087, Level 16, State 1, Procedure getLastFeatureUpdate, Line 19 [Batch Start Line 0]
Must declare the table variable "@tableName".
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64228888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档