不能在nvarchar上调用方法

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

解决方法:

您可能需要将用户定为[user].用户是保留关键字.

因此[User] .user_surname等

就像克里斯说的错字一样,将User.user_surname更改为Users.user_surname.

我猜无限的[用户]问题引起了奇怪的错误

上一篇:SQL Server 2000交叉选项卡多个表


下一篇:CodeGo.net>将T-SQL转换为LINQ-to-SQL