TextBox1和  TextBox2 均是我的两个文本域 我想把这两个文本域输进去的东西 分别记录到数据库的两个列中
但是 这样的代码 发现数据库并没有什么改变 ? 求指教 关于带参数的问题 
             string tx1 = TextBox1.Text;
            string tx2 = TextBox2.Text;
            SqlCommand command = new SqlCommand("INSERT INTO myuser VALUES(@tx1,@tx2)", con);
十分感谢

解决方案 »

  1.   

    应该是要这么写:
    "INSERT INTO (字段1,字段2) myuser VALUES(@tx1,@tx2)"
      

  2.   

    给你的 command 设置输入参数
      

  3.   

    SqlCommand command = new SqlCommand("INSERT INTO myuser(字段1,字段二) VALUES(
    " + "'" + tx1 + "','" + tx2 + "'"), con);
      

  4.   

    string tx1 = TextBox1.Text;
      string tx2 = TextBox2.Text;
      SqlCommand command = new SqlCommand("INSERT INTO myuser VALUES(@tx1,@tx2)", con);
      SqlParameter param=new SqlParameter("@tx1",SqlDbType.NvarChar,100);
      param.Value=tx1;
      command.Parameters.Add(param);
      param=new SqlParameter("@tx2",SqlDbType.NvarChar,100);
      param.Value=tx2;
      command.Parameters.Add(param);
      command.ExecuteNonQuery();
      

  5.   

    以上的所有方法 均没有用  a唉···SqlCommand command = new SqlCommand("INSERT INTO myuser (name,password) VALUES(" + "'" + tx1 + "','" + tx2 + "')",con);我的两个字段分别为name和password不知道该怎么办了
      

  6.   

    SqlCommand command = new SqlCommand("INSERT INTO myuser (name,password) VALUES(@tx1,@tx2)", con);
    command.Parameters.AddWithValue("@tx1", txt1);
    command.Parameters.AddWithValue("@tx2", txt2);
    command.ExecuteNonQuery();
    试一试~~
      

  7.   

    你想用参数化。你的代码又没有写全。。使用11楼的试试。应该没有问题。
    论坛签名======================================================================comeswat:你好!
    截至 2011-10-17 21:31:24 前:
    你已发帖 1 个, 未结贴 0 个;
    结贴率为: 100.00%

    当您的问题得到解答后请及时结贴.

    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
    http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
      

  8.   

    发个详细的。
    //首先定义连接字串。例子中的字串AdmsConnectionString是写在web.config中的。通过添加数据源可以直接生成该字串
     string strconn = ConfigurationManager.ConnectionStrings["AdmsConnectionString"].ConnectionString;
                SqlConnection cn = new SqlConnection(strconn);
                con.Open();
    //打开连接
    con.Open();
    //给变量赋值,建议加上Trim 函数去掉空格
    string tx1 = TextBox1.Text.Trim();
    string tx2 = TextBox2.Text.Trim();
    //定义sql(注意变量的单引号)引号问题可能导致sql失败
    SqlCommand command = new SqlCommand("INSERT INTO myuser (name,password) VALUES('tx1','tx2')", con);
    command.ExecuteReader();
    con.Close();试试以上的方案。祝成功