向数据库中插入数据出现的问题。帮忙给看看吧。
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,s易做图,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语句里出现了异常。可我不知道什么地方出现了异常~ --------------------编程问答-------------------- 你调试一下,到了SqlCommand mycomm = new SqlCommand(strsql, myconn);这一步,将sql语句放到sql Server里面执行一下,如果正确那么就是考虑是否打开了数据库,否则就是这条语句错了。有可能是数据类型问题。 --------------------编程问答-------------------- 连接数据库的语句好像不对。 --------------------编程问答--------------------
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 s易做图 = 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,s易做图,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, s易做图, 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错误还是数据库连接错误 --------------------编程问答-------------------- 恩,这样写就知道是哪里出错了。说的是。这从字符串向datetime转换时失败。我数据库里设置的sbirthtime和scome的数值类型都是datetime类型的。这里出现了问题。正在找解决办法呢。。 --------------------编程问答-------------------- '" + this.dateTimePicker1.Value.ToShortDateString() + "' 和 '" + this.dateTimePicker2.Value.ToShortDateString() + "' 把两对 单引号 去掉 --------------------编程问答-------------------- 如果你是做产品的话,请尽量不要这样写。
一是容易存错数据,二是容易被sql注入
补充:.NET技术 , C#