如何从 C# 网页调用具有多个参数的 SQL 函数
我有一个使用以下语法调用的 MS SQL 函数:
SELECT Field1, COUNT(*) AS RecordCount
FROM GetDecileTable('WHERE ClientID = 7 AND LocationName = ''Default'' ', 10)
第一个参数传递一个特定的 WHERE
子句,该函数将其用于一个内部查询。当我在前端 C# 页面中调用此函数时,我需要发送 WHERE
子句内各个字段的参数值(在本例中,两个 ClientID
& LocationName
字段)
当前的 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();
当我执行此操作时,我从 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'
执行此操作时,SQL 会抛出一个无法解析第一次提及的错误@ClientID
表示必须定义标量变量 @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'
我知道当我从表中选择数据时,这种添加参数并从 C# 调用 SQL 代码的方法效果很好,但我不确定如何将参数嵌入到嵌入式 WHERE
的 ' 引号内子句被传递给函数。
有什么想法吗?
I have an MS SQL function that is called with the following syntax:
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. 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)
The current C# code looks like this:
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:
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. 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.
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.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一部分
。
变量应该在字符串内部使用,它们被视为字符串的
was
should
your variables are used inside string where they are considered just as part of it.