新鲜的c#问题,开了看看了^^
一下代码提示:"连接未关闭 .连接的当前状态为打开!!"
private void button1_Click(object sender, EventArgs e)
{
//string a = label3Name.Text;//纪录员工姓名标签在.提示中显示1
string b = textBox6.Text;//获得"姓名"被修改;后的内容,赋值给b
string ba = textBox4.Text;//获得"部门"被修改;后的内容,赋值给BA
string bb = textBox2NO.Text;//获得"出生年月日"被修改后的内容,赋值给BB
string bc = textBox5.Text;//获得"电话号码"被修改后的内容,赋值给BC
int c = Convert.ToInt32(textBox3.Text);//获得学员ID
DialogResult choice = MessageBox.Show("确定要修改**?", "操作提示!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
try
{
if (choice == DialogResult.Yes)
{
int result = 0; //使用"b"得到的新值,更新数据库
string sql = string.Format("Update EmpInfo SET EmpName ='{0}',EmpDep = '{1}',EmpBirthday='{2}',EmpMobile='{3}' WHERE EmplD = {4}", b, ba, bb, bc, c);
DBHelpClass1.connection.Open();
SqlCommand Mycommand = new SqlCommand(sql, DBHelpClass1.connection);
result = Mycommand.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("修改成功!!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//以下为从新再次填充表框
try
{
int empID = (int)dataGridView1.CurrentRow.Cells["ID"].Value;
string sqlA = string.Format("SELECT EmplD,EmpName,EmpDep,EmpBirthday,EmpMobile FROM EmpInfo WHERE EmplD={0}", empID);
SqlCommand command = new SqlCommand(sql, DBHelpClass1.connection);
DBHelpClass1.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();//
if (!dataReader.HasRows)
{
MessageBox.Show("没有数据!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
while (dataReader.Read())
{
textBox3.Text = Convert.ToString(dataReader["EmplD"]);
textBox6.Text = Convert.ToString(dataReader["EmpName"]);
textBox4.Text = Convert.ToString(dataReader["EmpDep"]);
textBox2NO.Text = Convert.ToString(dataReader["EmpBirthday"]);
textBox5.Text = Convert.ToString(dataReader["EmpMobile"]);
}
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
DBHelpClass1.connection.Close();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
DBHelpClass1.connection.Close();
}
}