小弟最近在修改毕业设计的时候想用button列来实现每点击一次button就将当前datagridview的行记录插入到数据库的表中,想在CellContentClick事件里实现,但是程序运行起来就有问题,一点击button程序整个就自动关闭了(用messagebox测试的时候就没问题,程序不会关闭),想请教各大神是什么原因呢?需要新开线程来处理sql的操作吗?
我设计的代码如下,希望各位大神指点问题所在。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==4)
{
string str = "server=localhost;database=ts_sell;uid=sa;pwd="; string cid = "DD" + DateTime.Now.ToString("yyyyMMddhhmm");
SqlConnection scon = new SqlConnection(str);
scon.Open();
string sql = "insert into t_dingdan values ('" +cid + "','" + dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "','" + user + "')";
SqlCommand scom = new SqlCommand(sql, scon);
scom.ExecuteNonQuery();
MessageBox.Show("保存成功");
scon.Close();
}
}
我设计的代码如下,希望各位大神指点问题所在。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==4)
{
string str = "server=localhost;database=ts_sell;uid=sa;pwd="; string cid = "DD" + DateTime.Now.ToString("yyyyMMddhhmm");
SqlConnection scon = new SqlConnection(str);
scon.Open();
string sql = "insert into t_dingdan values ('" +cid + "','" + dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "','" + user + "')";
SqlCommand scom = new SqlCommand(sql, scon);
scom.ExecuteNonQuery();
MessageBox.Show("保存成功");
scon.Close();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货