逗号附近的语法不正确

逗号附近的语法不正确

这是我尝试从 session 中获取多个值时的 ASPX 代码片段。我收到一个错误:“逗号附近的语法不正确”(标记了代码段中的行):

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]), con);
        SqlDataReader dr1 = cmd1.ExecuteReader();
        var yourlist =new List<Int32>();
        if (dr1.HasRows)
        {
            while (dr1.Read())
            {
                yourlist.Add(Convert.ToInt32(dr1[0]));
            }
        }

        //String str1 = String.Join(", ", yourlist.Select(o => o.ToString()).ToArray());
            dr1.Close();
        cmd1.Dispose();
        Array k= yourlist.ToArray();
        Int32 a =Convert.ToInt32( k.GetValue(0));
        Int32 b =Convert.ToInt32( k.GetValue(1));
        Int32 c =Convert.ToInt32( k.GetValue(2));
        Int32 d =Convert.ToInt32( k.GetValue(3));
        SqlCommand cmd2 = new SqlCommand("select id,name from plugins where id =(" + a + " or " + b + " or " + c + " or " + d +  ")" , con); /// Error here?
        SqlDataReader dr2 = cmd2.ExecuteReader(); ///Error here?
        if (dr2.HasRows)
        {
            while (dr2.Read())
            {
                ListBox2.DataSource = dr2;
                ListBox2.DataBind();
            }
        }
        dr2.Close();
        cmd2.Dispose();
con.Close();

我错过了什么?

最佳答案

SQL 查询错误。将其更改为:

    SqlCommand cmd2 = new SqlCommand("select id,name from plugins
where id in(" + a + " , " + b + " , " + c + " , " + d +  ")" , con);

关于asp.net - 逗号附近的语法不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4640200/

10-11 17:34