2015-10-24 105 views
0

我的查詢是完美的(我已經在SQL Server Management Studio中對其進行了驗證)。我的代碼是完美的,還是我得到這個語法錯誤:Command.ExecuteNonQuery();錯誤:'='附近的語法不正確

Incorrect syntax near '='. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.

public partial class Temporaryche : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     ddlTDept.Items.Clear(); 
     ddlTBranch.Items.Clear(); 

     string connectionString = GlobalVariables.databasePath; 
     SqlConnection sqlCon = new SqlConnection(connectionString); 
     string query = "select fac.fac_name, dp.dp_name, br.br_name from STUDENT s, DIVISON dv, BRANCH br, DEPT dp, FACULTY fac, CLASS cls, DEGREE dg where dg.dg_id = cls.dg_id and cls.cls_id = s.cls_id and fac.fac_id = dp.fac_id and dp.dp_id = br.dp_id and br.br_id = dv.br_id and s.dv_id = dv.dv_id and s.prn_no = " + txtSearch.Text; 

     sqlCon.Open(); 
     SqlCommand cmd = new SqlCommand(query, sqlCon); 
     SqlDataReader reader = cmd.ExecuteReader(); 

     string facultyName = reader.GetValue(0).ToString(); 
     string deptName = reader.GetValue(1).ToString(); 
     string branchName = reader.GetValue(2).ToString(); 

     ddlTFaculty.SelectedValue = facultyName; 

     query = "select dp_name from DEPT where fac_id=(select fac_id where fac_name='" + facultyName + "')"; 
     cmd = new SqlCommand(query, sqlCon); 
     reader = cmd.ExecuteReader(); 
     ddlTDept.Items.Clear(); 

     while (reader.Read()) 
     { 
      ddlTDept.Items.Add(reader.GetValue(0).ToString()); 
     } 

     ddlTDept.SelectedValue = deptName; 
     sqlCon.Close(); 
    } 
} 
+0

我的錯誤是這樣的: 附近有語法錯誤「=」。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.Data.SqlClient.SqlException:'='附近的語法不正確。 – AlexR

+4

您應該使用SQL參數,而不是將facultyName內聯到SQL查詢中。否則,你很容易受到SQL注入攻擊。 https://開頭XKCD。com/327/ –

+2

[壞習慣踢:使用舊式聯接](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old- style-joins.aspx) - 在ANSI - ** 92 ** SQL標準(**超過20年)中,舊式*逗號分隔的表*樣式列表已替換爲* proper * ANSI'JOIN'語法**前),其使用是不鼓勵 –

回答

1

你不提發生了誤差,就這麼硬線是100%肯定,還有其他一些信息丟失。然而,在第一個SQL文本您在在結束後:

s.prn_no=" + txtSearch.Text; 

自認爲是文本應該在引號「」

s.prn_no='" + txtSearch.Text +"'" 

那說你不應該建立這樣的SQL查詢由於SQL Injection。相反,你應該使用Parameter objects

4

你不必在你的代碼中的任何的ExecuteNonQuery,所以其中一個語法錯誤可能發生在這條線

query = @"select dp_name from DEPT 
     where fac_id=(select fac_id where fac_name='" + facultyName + "')"; 

在此行的唯一一點你錯過了從子查詢,所以你應該寫爲

query = @"select dp_name 
      from DEPT 
      where fac_id= (select fac_id 
         FROM DEPT where fac_name= '" + facultyName + "')"; 

當然這只是解決您的直接問題,但正如其他人所說的,您應該立即開始使用參數化查詢。

例如

query = @"SELECT dp_name FROM DEPT 
      wHERE fac_id = (SELECT TOP 1 fac_id 
          FROM DEPT 
          WHERE [email protected]"; 
cmd = new SqlCommand(query, sqlCon); 
cmd.Parameters.Add("@faculty", SqlDbType.NVarChar).Value = facultyName; 
.... 

編輯
我要改寫我的第一個語句。還有一點可能會出現語法錯誤。它在第一行如果txtSearch.Text是空的。
在這種情況下,查詢保持不完整並觸發語法錯誤。
發生這種情況可能是因爲在文本框中仍然沒有任何內容時,在Page_Load事件中調用該代碼。所以,也許我們應該添加一些東西,以防止整個代碼塊的執行,如果txtSearch是空

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!string.IsNullOrWhiteSpace(txtSearch.Text)) 
    { 
     .... code that executes the queries ... 
    } 
} 

總而言之這個代碼在Page_Load事件不合身,也許這代碼應該移到當用戶請求進行搜索按鈕單擊事件....

最後,在評論中提到,你應該使用JOIN語法,以便更好地從你的表之間的關係劃分WHERE條件。 查詢可以寫成

SELECT fac.fac_name, 
     dp.dp_name, 
     br.br_name 
FROM STUDENT s INNER JOIN DIVISION dv ON s.dv_id = dv.dv_id 
     INNER JOIN BRANCH br ON dv.br_id = br.br_id 
     INNER JOIN DEPT dp ON br.dp_id = dp.dp_id 
     INNER JOIN FACULTY fac ON dp.fac_id = fac.fac_id 
     INNER JOIN CLASS cls ON s.cls_id = cls.cls_id 
     INNER JOIN DEGREE dg ON cls.dg_id = dg.dg_id 
WHERE [email protected]"; 
+0

那麼大概你有一點關於TOP 1 – Steve

+0

完成,但請不要刪除評論。這是對的,你應該讓它讓未來的讀者理解答案的真實性。 – Steve

+0

SqlCommand cmd = new SqlCommand(query,sqlCon); – AlexR

相關問題