private void button1_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source=.;Initial Catalog=lichang;Integrated Security=True";
            //string sqlconnstr=ConfirurationManager.ConnectionStrings["ConnectionString"].ConnectionStrings;
            SqlConnection connect = new SqlConnection(connString);
            SqlCommand sqlcommand = new SqlCommand();
            sqlcommand.Connection = connect;
            sqlcommand.CommandText = "insert into login(user,password,xt,ck,name,age,phonenum,sex)";
            sqlcommand.Parameters.AddWithValue("@user",textBox1.Text);
            sqlcommand.Parameters.AddWithValue("@password", textBox2.Text);
            sqlcommand.Parameters.AddWithValue("@xt", checkBox1.Checked);
            sqlcommand.Parameters.AddWithValue("@ck", checkBox2.Checked);
            sqlcommand.Parameters.AddWithValue("@name", textBox3.Text);
            sqlcommand.Parameters.AddWithValue("@age", textBox6.Text);
            sqlcommand.Parameters.AddWithValue("@phonenum", textBox4.Text);
            sqlcommand.Parameters.AddWithValue("@sex", textBox5.Text);
            try
            {
                connect.Open();
                sqlcommand.ExecuteNonQuery();
                MessageBox.Show("成功添加!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception)
            {
                MessageBox.Show("错误原因!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            finally
            {
                sqlcommand = null;
                connect.Close();
                connect = null;
            }
        }

解决方案 »

  1.   

    sqlcommand.CommandText = "insert into login(@user,@password,@xt,@ck,@name,@age,@phonenum,@sex)";
      

  2.   

    insert into login(user,password,xt,ck,name,age,phonenum,sex)values(@user,@password,@xt,@ck,@name,@age,@phonenum,@sex)这是整个sql
      

  3.   

    sqlcommand.CommandText = "insert into login(user,password,xt,ck,name,age,phonenum,sex)   values(@user,@password,@xt,@ck,@name,@age,@phonenum,@sex)
    ";
     
      

  4.   

    sqlcommand.CommandText = "insert into login(user,password,xt,ck,name,age,phonenum,sex)values(@user,@password,@xt,@ck,@name,@age,@phonenum,@sex)";
    我就是这样写的啊
      

  5.   

    INSERT INTO table_name (列1, 列2,...) VALUES (@值1, @值2,....)另外有没有报异常 异常信息是什么~
      

  6.   

    catch (Exception)
      {
      MessageBox.Show("错误原因!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      this.Close();
      }
     这改成 
    catch (Exception e)
      {
      MessageBox.Show("错误原因:"+ex.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      this.Close();
      }
    然后看错误原因
      

  7.   

    发错了 这样
    catch (Exception)
      {
      MessageBox.Show("错误原因!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      this.Close();
      }
     这改成  
    catch (Exception e)
      {
      MessageBox.Show("错误原因:"+e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
      this.Close();
      }
    然后看错误原因 
      

  8.   

    看楼主发的代码里面,insert的SQL语句写的不对,楼上的有大师回复过了,我就不写了
      

  9.   

    你的 xt  ck 数据库字段类型是什么  你可要看你在外面取值是  true or false  类型不对的话也插入不成功的 
      

  10.   

    sqlcommand.CommandText = "insert into login(user,password,xt,ck,name,age,phonenum,sex)";
      sqlcommand.Parameters.AddWithValue("@user",textBox1.Text);
      sqlcommand.Parameters.AddWithValue("@password", textBox2.Text);
      sqlcommand.Parameters.AddWithValue("@xt", checkBox1.Checked);
      sqlcommand.Parameters.AddWithValue("@ck", checkBox2.Checked);

    如果上面标红的字段你数据库不是字符串的话,会报错的,还有,你的sql语句没写完整,建议你写完整(楼上有人已经写过了,你复制过去)
      

  11.   

    XT KC 是BIT 类型   
      

  12.   

    那插入的时候应该是  0 和 1 吧 你插入 true false 会报错的  
      

  13.   

     sqlcommand.Parameters.AddWithValue("@xt", checkBox1.Checked==true?1:0);
     sqlcommand.Parameters.AddWithValue("@ck", checkBox2.Checked==true?1:0);
    这两个参数用这个方法添加值
      

  14.   

    调试一下,看是不是Sql语句的问题。