3

我在我的 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();
4

1 回答 1

4

您可能需要将用户分隔为 [user]。用户是保留关键字。

所以 [User].user_surname 等

同样正如克里斯所说,错字将 User.user_surname 更改为 Users.user_surname。

我猜未定界的 [User] 问题导致了奇怪的错误

于 2012-04-15T21:26:13.163 回答