2016-01-28 40 views
0

當我運行這段代碼erorr apeared關鍵字附近 不正確的語法,其中c#錯誤的語法附近的關鍵字在C#?

public SqlDataReader GetDR(CommandType HandelMode, string SQLStat, List<SqlParameter> Parms) 
{ 
    SqlDataReader R = null; 
    SqlCommand com = new SqlCommand(); 
    SqlConnection Con = GetConn(); 
    try 
    { 
     com.CommandText = SQLStat; 
     com.Connection = Con; 
     com.CommandType = HandelMode; 

     if (Parms != null) 
     { 
      foreach (SqlParameter P in Parms) 
      { 
       com.Parameters.Add(P); 
      } 
     } 

     R = com.ExecuteReader(CommandBehavior.CloseConnection); 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); 
     return null; 
    } 
    finally 
    { 
     Con.Close(); 
    } 
    return R; 
} 


private void pictureBox10_Click_2(object sender, EventArgs e) 
{ 

    List<SqlParameter> ParsList = new List<SqlParameter>(); 
    string SelectStatement = "Select ID,Aname,Ename,I_D from " + ScreenMasterTableName; 
    string Cond = " [email protected]_Co"; 
    ParsList.Add(new SqlParameter("@ID_Co", FormInfo.ID_Co)); 

    if (S_ID.Text != "") 
    { 
     decimal D = 0; 
     decimal.TryParse(S_ID.Text, out D); 
     Cond += " and [email protected]"; 
     ParsList.Add(new SqlParameter("@ID", D)); 
    } 
    if (S_Aname.Text != "") 
    { 
     if (Cond != "") 
      Cond += " and "; 
     Cond += " Aname [email protected]"; 
     ParsList.Add(new SqlParameter("@Aname", S_Aname.Text)); 
    } 

    if (S_Ename.Text != "") 
    { 
     if (Cond != "") 
      Cond += " and "; 
     Cond += " Ename [email protected]"; 
     ParsList.Add(new SqlParameter("@Ename", S_Ename.Text)); 
    } 
    if (Cond != "") 
     Cond = " where " + Cond; 


    var L = Bus.GetSearchedData(SelectStatement + Cond, ParsList); 
    dataGridView1.DataSource = L; 
    label9.Text = L.Count.ToString(); 
} 
+3

向我們展示由SelectStatement + Cond所做的最終查詢 – Adil

+0

第二個代碼是最終查詢提出的b y select + cond –

+0

你能得到你調試的查詢,並向我們展示查詢構造了什麼。 – Adil

回答

-2

假設你S_ID填入你會有這樣的查詢:

where and [email protected] 
+1

的列'Cond'作爲'string Cond =「ID_Co = @ ID_Co」;' –

+0

開始''你說得對,讓所有if cond ==「」檢查都是多餘的。 – Carra

相關問題