插入数据sql语句:
com.CommandText = " Insert into Student values('10','苍井空','25')"; 
我想问的是:括号内可不可以写变量。
比如:
string a = new string();
string b = new string();
string c = new string();com.CommandText = " Insert into Student values(a,b,c)"; 
这样写行不行?如果行,a、b、c加不加单引号呢?如果要加的话,到底加的是变量a,还是实际的字母a呢?

解决方案 »

  1.   

    可以用SqlParameter。像楼主那样用是不可能的。
      

  2.   

    string commandText = string.Format("Insert into Student values('{0}','{1}','{2}')",a,b,c);
    com.CommandText = commandText;
      

  3.   

    com.CommandText = " Insert into Student values(@a, @b, @c)";
    com.Parameters.AddWithValue("@a", a);
    com.Parameters.AddWithValue("@b", b);
    com.Parameters.AddWithValue("@c", c);
      

  4.   

    大致可以这样:int id = 10;
    string name = "苍井空";   // 这是谁,不认识
    int age = 25;string strCmd = @"insert into Student (id, name, age) values ("+id.ToString()+','+name+','+age.ToString()+')';SqlCommand cmd = new SqlCommand(strCmd, conn);不过建议用SqlParameter类来实现这样的参数,那个比这个灵活和方便。
      

  5.   

    你可以建个Student类来保存你要插入的字段,类里面的成员就是student表里面的字段.student stuInfo = new Student();
    //字符串就加'',不是字符串就不用'',直接"+stuInfo.字段+"
    com.CommandText = " Insert into Student values('"+stuInfo.字段+"','"+stuInfo.字段名+"')"; 
     
    然后在外面调用这个方法的时候把值这样传入student stuInfo = new Student()
    {
       stuInfo.字段名= txtbox1.text;
       stuInfo.字段名= txtbox2.text;
    };
    然后调用增加的方法参数就是stuInfo
      

  6.   

    我上面的串忘记给name加上单引号了,哈哈用string.Format()构造会更清晰一点。
      

  7.   

    sql = string.Format("insert into tlb ('{0}','{1}','{2}')",s1,s2,s3);or SqlParameter
      

  8.   

    student stuInfo = new Student()
    {
       stuInfo.字段名= txtbox1.text,
       stuInfo.字段名= txtbox2.text
    };看错,前面那个应该是这样写。
      

  9.   

    StringBuilder strSql=new StringBuilder();
    strSql.Append("insert into AV_Info(");
    strSql.Append("AVId,AV_Name,AV_Age)");
    strSql.Append(" values (");
    strSql.Append("@AVId,@AV_Name,@AV_Age)");
    SqlParameter[] parameters = {new SqlParameter("@AVId", SqlDbType.Int,4),
    new SqlParameter("@AV_Name", SqlDbType.VarChar,255),
    new SqlParameter("@AV_Age", SqlDbType.Int,4)};
    parameters[0].Value = model.AVId;
    parameters[1].Value = model.AV_Name;
    parameters[2].Value = model.AV_Age;
    DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
                          
      

  10.   

    com.CommandText = " Insert into Student values('"+a+"','"+b+"','"+c+"')"; 
      

  11.   

    string textBox1str ;
    foreach (Control c in groupBox1.Controls)
         {
            if (c is CheckBox)
                  {
                       if ((c as CheckBox).Checked && textBox1.Text != "")
                              {
                                  textBox1str = textBox1.Text;  //textBox1str已经赋值了啊
                              }
                   }
         }
    using (SqlCommand com = con.CreateCommand())
          {
           com.CommandText = " Insert into 开奖数据 values('"+textBox1str+"')";          //为什么报错?说使用了未赋值的textBox1str          
     com.ExecuteNonQuery();  //执行插入
                      }上面的textBox1str为什么说没有赋值呢?
      

  12.   

    声明变量的时候这样写:
    string textBox1str = "" ;
      

  13.   

    声明的时候一定要付初值
    string textBox1str=string.Empty ;
      

  14.   


    在带上数据类型,这样安全些,别的都没法防止SQL注入
      

  15.   

    com.CommandText = " Insert into Student values(@a, @b, @c)";
    com.Parameters.AddWithValue("@a", a);
    com.Parameters.AddWithValue("@b", b);
    com.Parameters.AddWithValue("@c", c);
      

  16.   

    com.CommandText = " Insert into Student values('10','苍井空','25')";  
    com.ExecuteNonQuery(); 忘了问:
    插入这么一句数据,是插在表中的哪个位置呢?没有指定插入的位置啊?不可能随便一行插入吧
      

  17.   

    如果你想使用变量的话,可以按照一下方式写,也就是利用""将字符串隔开,中间再用+将变量进行连接。
    string sql = string.Format("insert into student values(" + a + " ," + b + ")");
    你也可以使用string类的Format()方法中的占位符,如下string sql = string.Format("insert into student values('{0}','{1}')",a,b);注意,以上如果变量为字符串类型,需要将占位符用单引号括起来。但是推荐使用SqlParameter,楼上已经说都很详细了。
      

  18.   

    我想请问一下美女,这个知识点在MSDN的哪个地方,我想找到出处,请给一个MSDN链接
    谢谢
      

  19.   

    可以用变量,那样就成了字符串的拼接。还是像大家建议那样用SqlParameter
      

  20.   

    index  new string a="name"
    at 属性列;