id是自动生成,不插ID;只插tName,tSex
sqlCommand的CommandText的语句是:INSERT INTO t_user(name, sex)
                       VALUES (@tName, @tSex)
代码:
1.cmdInsert.CommandType=CommandType.StoredProcedure;
2.cmdInsert.Parameters.Add("@tName",TextBox1.Text);
3.cmdInsert.Parameters.Add("@tSex",TextBox2.Text);4. conn.Open();    
5. cmdInsert.ExecuteNonQuery();
为什么插入不进去???
是2行和3行的错误吗???
cmdInsert.Parameters["@tName"].Value=TextBox1.Text;这样赋值也插不进啊

解决方案 »

  1.   

    cmdInsert.CommandType=CommandType.StoredProcedure; 
    去掉
    你的sql语句不是存储过程
      

  2.   

    con.open();
    SqlCommand cmd=new SqlCommand("INSERT INTO  t_user(name,   sex) VALUES('name', 'tSex'),con);  //con是数据库连接
    //name和tSex是你想要插入的值,
    cmd.ExecuteNonQuery(); 
    con.close();
      

  3.   

    去掉cmdInsert.CommandType=CommandType.StoredProcedure;   
    还是不对
      

  4.   

    你那是参数化sql语句,去掉cmdInsert.CommandType=CommandType.StoredProcedure; 当然还是要报错的      
    你照我的写,保你没错!
            conn.Open();    
             comm=new SqlCommand();
            comm.CommandText="insert into t_user(name,   sex) VALUES   (@tName,   @tSex) ";
            comm.Open();//你也没打开comm
            SqlParameter[] par = new SqlParameter[]
            {
                new SqlParameter("@tName",SqlDbType.参数类型,大小),
                new SqlParameter("@tSex",SqlDbType.参数类型,大小),
            };
            par[0].Value = 赋值;
            par[1].Value = 赋值;
            //循环压参数
             for (int i = 0; i < par.Length; i++)
            {
                comm.Parameters.Add(par[i]);
            }
            comm.ExecuteNonQuery();
            conn.Close();
      

  5.   

    谢谢fanaaa3 !这代码看着就舒服,