无法在nvarchar上调用方法

时间:2012-04-15 21:17:15

标签: c# asp.net sql-server-2008 tsql

我的简单SELECT语句有奇怪的问题,我在asp.net应用程序中使用它。我使用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();

1 个答案:

答案 0 :(得分:4)

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

所以[用户] .user_surname等

另外,正如Chris所说,拼写错误将User.user_surname更改为Users.user_surname。

我认为无限[User]问题导致了奇怪的错误