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;
}
}
{
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;
}
}
";
我就是这样写的啊
{
MessageBox.Show("错误原因!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
这改成
catch (Exception e)
{
MessageBox.Show("错误原因:"+ex.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
然后看错误原因
catch (Exception)
{
MessageBox.Show("错误原因!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
这改成
catch (Exception e)
{
MessageBox.Show("错误原因:"+e.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
然后看错误原因
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语句没写完整,建议你写完整(楼上有人已经写过了,你复制过去)
sqlcommand.Parameters.AddWithValue("@ck", checkBox2.Checked==true?1:0);
这两个参数用这个方法添加值