我在我的 asp.net 应用程序中使用的简单 SELECT 语句有一个奇怪的问题。我使用 MSSQL 2008。
该声明有效:
SelectSQL = "SELECT user_id, user_name, user_surname, user_code FROM Users WHERE user_group = '" + drop.SelectedItem.Value + "'";
但是,此行会抛出“无法在 nvarchar 上调用方法。 ”
SelectSQL = "SELECT COUNT(DISTINCT Equations.eq_id) AS pocet_prikladu, Users.user_name, User.user_surname FROM Users LEFT JOIN Equations ON (Users.user_id = Equations.eq_user_id) WHERE Users.user_code = '" + drop.SelectedItem.Value + "' GROUP BY Users.user_id, Users.user_name, User.user_surname ";
这是更多代码,它停在最后一行
public void FillTable(Table tab, DropDownList drop) //naplneni tabulky
{
SqlConnection pripojeni = new SqlConnection(connectionString);
string SelectSQL = "";
if (action == "groups")
{
SelectSQL = "SELECT user_id, user_name, user_surname, user_code FROM Users WHERE user_group = '" + drop.SelectedItem.Value + "'";
}
else
{
SelectSQL = "SELECT COUNT(DISTINCT Equations.eq_id) AS pocet_prikladu, Users.user_name, User.user_surname FROM Users LEFT JOIN Equations ON (Users.user_id = Equations.eq_user_id) WHERE Users.user_code = '" + drop.SelectedItem.Value + "' GROUP BY Users.user_id, Users.user_name, User.user_surname ";
}
try
{
SqlCommand prikaz = new SqlCommand(SelectSQL, pripojeni);
pripojeni.Open();
SqlDataReader vysledky = prikaz.ExecuteReader();