我在我的asp.net應用程序中使用簡單的SELECT語句有奇怪的問題。我與MSSQL工作,2008年無法在nvarchar上調用方法
此語句的工作原理:
SelectSQL = "SELECT user_id, user_name, user_surname, user_code FROM Users WHERE user_group = '" + drop.SelectedItem.Value + "'";
然而,這條線拋出
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 ";
這裏更多的代碼,它停止 「不能調用nvarchar的方法」。在最後一行
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();
能否請您發佈更完整的代碼示例,包括你如何將查詢發送到SQL? – jklemmack 2012-04-15 21:22:07
你有一個錯字 - User.user_surname應該是Users.user_surname – 2012-04-15 21:27:10
謝謝克里斯。它現在有效。通過尋找解決方案花費的另一個浪費時間:D – polohy 2012-04-15 21:29:45