写了个update语句。想把列名写成变量。能循环执行的。该怎样写啊。搞不出来列名应该是date1,date2....
          
            string m = n.ToString();
            string s =Convert .ToString (7) ;            
            string x = s + ":" + m;
            int da=2;
            string dat="date"+da.ToString ();
            string sql = " update attend set  dat  = '" + x + "'";

解决方案 »

  1.   

     string sql = " update attend set"+ dat +"= '" + x + "'";
      

  2.   

    string sql = " update attend set "+ dat +" = '" + x + "'";
      

  3.   

    自己改改吧,你的我没看懂啥意思。StringBuilder builder = new StringBuilder("update attend set ");
    string[] values = new string[] { "x1", "x2", "x3", "x4", "x5",};
    for (int i = 1; i <= 5; i++)
    {
        builder.AppendFormat("date{0}={1}", new object[] { i.ToString(), values[i - 1] });
        if (i < 5)
        {
            builder.Append(",");
        }
    }
    Console.WriteLine(builder.ToString());
      

  4.   

    why not use stringbuild ?make the column name before the SQL.
      

  5.   


    动态sql。dat 是一个参数,也要和 x 一样当做参数使用。string sql = " update attend set  " +dat + " = '" + x + "'";
    相当于 
    string sql = string.formart(" update attend set  {0} = '{1}' ", dat, x);