简体   繁体   English

如何从C#网页调用具有多个参数的SQL函数

[英]How to call SQL Function with multiple parameters from C# web page

I have an MS SQL function that is called with the following syntax: 我有一个使用以下语法调用的MS SQL函数:

SELECT Field1, COUNT(*) AS RecordCount
FROM GetDecileTable('WHERE ClientID = 7 AND LocationName = ''Default'' ', 10)

The first parameter passes a specific WHERE clause that is used by the function for one of the internal queries. 第一个参数传递一个特定的WHERE子句,该子句由函数用于内部查询之一。 When I call this function in the front-end C# page, I need to send parameter values for the individual fields inside of the WHERE clause (in this example, both the ClientID & LocationName fields) 当我在前端C#页面中调用此函数时,我需要为WHERE子句内的各个字段发送参数值(在此示例中, ClientIDLocationName字段均是如此)

The current C# code looks like this: 当前的C#代码如下所示:

String SQLText = "SELECT Field1, COUNT(*) AS RecordCount FROM GetDecileTable('WHERE
                  ClientID = @ClientID AND LocationName = @LocationName ',10)";
SqlCommand Cmd = new SqlCommand(SQLText, SqlConnection);
Cmd.Parameters.Add("@ClientID", SqlDbType.Int).Value = 7; // Insert real ClientID
Cmd.Parameters.Add("@LocationName", SqlDbType.NVarChar(20)).Value = "Default";
               // Real code uses Location Name from user input
SqlDataReader reader = Cmd.ExecuteReader();

When I do this, I get the following code from SQL profiler: 当我这样做时,我从SQL事件探查器获得以下代码:

exec sp_executesql N'SELECT Field1, COUNT(*) as RecordCount FROM GetDecileTable
(''WHERE ClientID = @ClientID AND LocationName = @LocationName '',10)',
N'@ClientID int,@LocationID nvarchar(20)',
@ClientID=7,@LocationName=N'Default'

When this executes, SQL throws an error that it cannot parse past the first mention of @ClientID stating that the Scalar Variable @ClientID must be defined. 执行此操作时,SQL会引发错误,它无法解析第一次提及@ClientID发出的错误,指出必须定义标量变量@ClientID If I modify the code to declare the variables first (see below), then I receive an error at the second mention of @ClientID that the variable already exists. 如果我修改代码以首先声明变量(请参见下文),则在第二次提及@ClientID会收到错误@ClientID ,表明该变量已存在。

exec sp_executesql N'DECLARE @ClientID int; DECLARE @LocationName nvarchar(20); 
SELECT Field1, COUNT(*) as RecordCount FROM GetDecileTable
(''WHERE ClientID = @ClientID AND LocationName = @LocationName '',10)',
N'@ClientID int,@LocationName nvarchar(20)',
@ClientID=7,@LocationName=N'Default'

I know that this method of adding parameters and calling SQL code from C# works well when I am selecting data from tables, but I am not sure how to embed parameters inside of the ' quote marks for the embedded WHERE clause being passed to the function. 我知道,当我从表中选择数据时,这种从C#添加参数并调用SQL代码的方法效果很好,但是我不确定如何将参数嵌入到传递给函数的嵌入式WHERE子句的'引号内。

Any ideas? 有任何想法吗?

was 原为

SELECT Field1, COUNT(*) AS RecordCount FROM GetDecileTable('WHERE
ClientID = @ClientID AND LocationName = @LocationName ',10)

should 应该

SELECT Field1, COUNT(*) AS RecordCount FROM GetDecileTable('WHERE
ClientID = ' + @ClientID + ' AND LocationName = ' + @LocationName,10)

your variables are used inside string where they are considered just as part of it. 您的变量会在字符串内部使用,在字符串中会被视为变量的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM