private void button1_Click(object sender, EventArgs e)
{
string strcon = "Server=(local);Integrated Security=true;database=stu_info_manage";
try
{
string strsql = "insert into stu_info_manage(sno,sname,ssex,sbirthtime,snation,sdept,smajor,sclass,scome,sphone,saddress,scomment) values('" + this.textBox1.Text.Trim() + "','" + this.textBox2.Text.Trim() + "','" + this.comboBox1.Text.Trim() + "','" + this.textBox4.Text.Trim() + "','" + this.dateTimePicker1.Value.ToShortDateString() + "','" + this.textBox6.Text.Trim() + "','" + this.textBox7.Text.Trim() + "','" + this.textBox8.Text.Trim() + "','" + this.dateTimePicker2.Value.ToShortDateString() + "','" + this.textBox9.Text.Trim() + "','" + this.textBox10.Text.Trim() + "','" + this.richTextBox1.Text.ToString() + "')";
SqlConnection myconn = new SqlConnection(strcon);
myconn.Open();
SqlCommand mycomm = new SqlCommand(strsql, myconn);
int i = mycomm.ExecuteNonQuery();
myconn.Close();
if (i > 1)
{
MessageBox.Show("插入成功!");
}
}
catch (Exception ex)
{
MessageBox.Show("请全部填写,内容不能为空。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}总是出现“请全部填写,内容不能为空”。应该是try语句里出现了异常。可我不知道什么地方出现了异常~
{
string strcon = "Server=(local);Integrated Security=true;database=stu_info_manage";
try
{
string strsql = "insert into stu_info_manage(sno,sname,ssex,sbirthtime,snation,sdept,smajor,sclass,scome,sphone,saddress,scomment) values('" + this.textBox1.Text.Trim() + "','" + this.textBox2.Text.Trim() + "','" + this.comboBox1.Text.Trim() + "','" + this.textBox4.Text.Trim() + "','" + this.dateTimePicker1.Value.ToShortDateString() + "','" + this.textBox6.Text.Trim() + "','" + this.textBox7.Text.Trim() + "','" + this.textBox8.Text.Trim() + "','" + this.dateTimePicker2.Value.ToShortDateString() + "','" + this.textBox9.Text.Trim() + "','" + this.textBox10.Text.Trim() + "','" + this.richTextBox1.Text.ToString() + "')";
SqlConnection myconn = new SqlConnection(strcon);
myconn.Open();
SqlCommand mycomm = new SqlCommand(strsql, myconn);
int i = mycomm.ExecuteNonQuery();
myconn.Close();
if (i > 1)
{
MessageBox.Show("插入成功!");
}
}
catch (Exception ex)
{
MessageBox.Show("请全部填写,内容不能为空。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}总是出现“请全部填写,内容不能为空”。应该是try语句里出现了异常。可我不知道什么地方出现了异常~
private void button1_Click(object sender, EventArgs e)
{
string strcon = "Server=(local);Integrated Security=true;database=stu_info_manage";
try
{
string sno = this.textBox1.Text.Trim();
string sname = this.textBox2.Text.Trim();
string ssex = this.comboBox1.Text.Trim();
string sbirthtime = this.textBox4.Text.Trim();
string snation = this.dateTimePicker1.Value.ToShortDateString();
string sdept = this.textBox6.Text.Trim();
string smajor = this.textBox7.Text.Trim();
string sclass = this.textBox8.Text.Trim();
string scome = this.dateTimePicker2.Value.ToShortDateString();
string sphone = this.textBox9.Text.Trim();
string saddress = this.textBox10.Text.Trim();
string scomment = this.richTextBox1.Text.ToString(); StringBuilder strsql = new StringBuilder("insert into stu_info_manage(sno,sname,ssex,sbirthtime,snation,sdept,smajor,sclass,scome,sphone,saddress,scomment) ");
strsql.AppendFormat(" values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}') ", sno, sname, ssex, sbirthtime, snation, sdept, smajor, sclass, scome, sphone, saddress, scomment);
SqlConnection myconn = new SqlConnection(strcon);
myconn.Open();
SqlCommand mycomm = new SqlCommand(strsql.ToString(), myconn);
int i = mycomm.ExecuteNonQuery();
myconn.Close();
if (i > 1)
{
MessageBox.Show("插入成功!");
}
}
catch (Exception ex)
{
//MessageBox.Show("请全部填写,内容不能为空。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show(ex.Message);//看看原始异常信息到底是什么?
}
}
你的sql语句不要写在一行,不然无法判断到底是从哪一个控件中取值失败,另外要在代码中设置断点进行调试,不然怎么可能知道bug到底出在什么地方。你无法判断到底是拼接处理的sql错误还是数据库连接错误
一是容易存错数据,二是容易被sql注入