【发布时间】:2010-10-19 00:18:52
【问题描述】:
我正在使用ObjectDataSource 分页数据,我有以下方法:
public int GetNumberOfArticles(string employeeIds)
{
System.Data.DataTable dataTable;
System.Data.SqlClient.SqlDataAdapter dataAdapter;
System.Data.SqlClient.SqlCommand command;
int numberOfArticles = 0;
command = new System.Data.SqlClient.SqlCommand();
command.Connection = Classes.Database.SQLServer.SqlConnection;
command.CommandText = @"SELECT COUNT(*)
FROM
[Articles]
WHERE
[Articles].[EmployeeID] IN (@EmployeeIds)";
command.Parameters.AddWithValue("@EmployeeIds", employeeIds);
numberOfArticles = (int)command.ExecuteScalar();
return numberOfArticles;
}
EmployeeID 是一个整数,因此,我放在employeeIds 中的任何内容都将转换为整数。但是,因为我使用的是 IN 关键字,很明显我想用逗号分隔的 id 列表替换 employeeIds:
1, 2, 3
但是当我换行时:
command.Parameters.AddWithValue("@EmployeeIds", employeeIds);
类似:
command.Parameters.AddWithValue("@EmployeeIds", "1, 2, 3");
我收到一个异常,因为我提供了一个字符串,而 EmployeeIds 是一个整数。那么,我该怎么做呢?
谢谢。
编辑:
从回复中,我知道这必须手动完成,但是包含此方法的类将由ObjectDataSource 自动创建。那么如何在运行时提供employeeIds 的值呢?
【问题讨论】:
标签: c# parameters sqlcommand sql