错误的语法附近的关键字在C#?

问题描述:

当我运行这段代码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

假设你S_ID填入你会有这样的查询:

where and [email protected] 
+1

的列'Cond'作为'string Cond =“ID_Co = @ ID_Co”;' –

+0

开始''你说得对,让所有if cond ==“”检查都是多余的。 – Carra