就会发生错误
这是代码:
if (dataGridView1.SelectedRows.Count > 0)
{
}
else
{
MessageBox.Show("请选择要的修改的行");
return;
}
string str1 = this.button2.Text;
//获取输入的信息
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string stime = Convert.ToDateTime(this.dateTimePicker1.Text).ToString("yyyy-MM-dd");
string etime = "";
if(this.dateTimePicker2.Visible == true)
{
etime = Convert.ToDateTime(this.dateTimePicker2.Text).ToString("yyyy-MM-dd");
}
string xuehao = this.textBox2.Text;
string xuhao = this.textBox3.Text;
string yuanyin = this.richTextBox1.Text;
//编写修改的sql语句
string strsql = "update kqlog set starttime='"+stime+"',"+
"enttime='"+etime+"',"+
"yuanlin='"+yuanyin+"',"+
"xuhao='"+xuhao+"',"+
"xuehao='"+xuehao+"')";
//...用id当条件就行,不用做这么麻烦了
strsql += " where id='" + id + "'";
// 添加异常处理
try
{
SqlConnection con = GetCon();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = strsql;
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
this.toolStripStatusLabel1.Text = "操作成功 !";
}
con.Close();
shuxin();
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = "操作失败!失败原因:" + ex.Message;
}
}