c# - 尝试在 C# 中传递 2 个参数时,SQL Server 存储过程或函数指定了太多参数
问题描述
我正在尝试执行一个名为getLastFeatureUpdate
.
我将在下面逐步解释问题:
我在 SQL 中创建了一个表,如下所示:
CREATE TABLE testTable
(
DayTime INT NOT NULL, /*yyddhhmm, 1010102345*/
FeatureNbr SMALLINT NOT NULL,
Val FLOAT(53) NOT NULL
);
我现在创建了一个名为getLastFeatureUpdate
. 这里需要注意的重要一点是,我使用了 2 个参数@maxDateTime
和@tableName
,因为它们每次都不同。因此,我将在最后的 C# 代码中传递这两个参数。
存储过程(如果我@tableName text
从过程和 C# 代码中删除。那么代码确实可以提及)
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
,代码将起作用(然后我必须@tableName
在存储过程中也将其作为参数删除getLastFeatureUpdate
)
cmd.Parameters.Add(new SqlParameter("@maxDateTime", 10102248));
cmd.Parameters.Add(new SqlParameter("@tableName", "testTable")); //If not using this parameter, the code will work
C#代码:
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";
}
解决方案
您无法在 SQL Server 中参数化表名,因此:该 SQL 无效,CREATE PROC
实际上并没有运行。旧proc的内容是什么:只有你知道,但是:它不是显示的代码。它可能是您在开发的某个阶段拥有的虚拟版本。尝试输入:
exec sp_helptext getLastFeatureUpdate;
具体来说,服务器应该告诉你:
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".
推荐阅读
- javascript - 刽子手游戏。我把逻辑搞砸了,但我似乎无法让它工作......关于我错过了什么或做错了什么的任何指针?
- c# - IdentityServer4 使用 ApiKey 或 Basic 直接对 API 进行身份验证
- java - 如何在类中包含 main 方法,其中包含在 main 中调用的实例的定义?
- wpf - 发布时如何只有最新版本
- javascript - 在nodejs应用程序中写入两次后JSON文件为空
- python-3.x - Odoo 12:如何防止执行默认字段方法
- javascript - Angular 10添加数字
- flutter - String 不是 int 的子类型
- java - 不显示堆栈中的元素
- javascript - Redux 中的递归函数