我故意在txtbxemail.text中输入了错误的输入后,按下更新按钮,然后显示我创建的消息框。然后我更正了txtbxemail.text中的textinut并再次按下更新按钮。
然后它显示"连接已经打开"。
需要帮助和解决方案的详细说明。
我的代码:
private void button2_Click(object sender, EventArgs e)
{
if (MyMessageBox.ShowMessage("You are about to update an important record!", "ARE YOU SURE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
MemoryStream mss = new MemoryStream();
pictureBox1.Image.Save(mss, pictureBox2.Image.RawFormat);
byte[] imgg = mss.ToArray();
MySqlCommand cmnd = new MySqlCommand("update rfidnew.rfidemployees set RFIDnum=@rfidnum,firstname=@fn,lastname=@ln,department=@dep,position=@posi,emailadd=@eadd,cellphone=@cp,licenseno=@licnse,platenumber=@pnumb,employeeimage=@eimage,vehicleimage=@vimage where id = @ID", myConn);
cmnd.Parameters.Add("@ID", MySqlDbType.Int32).Value = txtbxid.Text;
cmnd.Parameters.Add("@rfidnum", MySqlDbType.VarChar).Value = txtbxrfid.Text;
cmnd.Parameters.Add("@fn", MySqlDbType.VarChar).Value = txtbxfn.Text;
cmnd.Parameters.Add("@ln", MySqlDbType.VarChar).Value = txtbxln.Text;
cmnd.Parameters.Add("@dep", MySqlDbType.VarChar).Value = txtbxdep.Text;
cmnd.Parameters.Add("@posi", MySqlDbType.VarChar).Value = cmboboxpos.Text;
cmnd.Parameters.Add("@eadd", MySqlDbType.VarChar).Value = txtbxemail.Text;
cmnd.Parameters.Add("@cp", MySqlDbType.VarChar).Value = txtbxcp.Text;
cmnd.Parameters.Add("@licnse", MySqlDbType.VarChar).Value = txtbxlic.Text;
cmnd.Parameters.Add("@pnumb", MySqlDbType.VarChar).Value = txtbxplate.Text;
cmnd.Parameters.Add("@eimage", MySqlDbType.Blob).Value = img;
cmnd.Parameters.Add("@vimage", MySqlDbType.Blob).Value = imgg;
MySqlDataReader myrdr;
Regex rx = new Regex("\\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~=]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9]*[a-z0-9])?)\\Z");
try
{
myConn.Open();
if (!rx.IsMatch(txtbxemail.Text))
{
MyMessageBox.ShowMessage("Invalid format in Email Add field", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information);
myConn.Close();
txtbxemail.Clear();
txtbxemail.Focus();
auto_load_table();
}
else
{
myrdr = cmnd.ExecuteReader();
ExecMyQuery(cmnd, "You are trying to update a record");
auto_load_table();
View_all_data disvid = new View_all_data();
this.Dispose();
disvid.Show();
while (myrdr.Read())
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
myConn.Close();
}
}
答案 0 :(得分:1)
完成数据库操作后应关闭连接。在您的情况下,您应该使用MySqlConnection
块创建using
的新实例,然后打开并关闭连接。
using(var myConn = new MySqlConnection("yourConnectionString"))
{
try
{
myConn.Open();
if (!rx.IsMatch(txtbxemail.Text))
{
MyMessageBox.ShowMessage("Invalid format in Email Add field", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information);
myConn.Close();
txtbxemail.Clear();
txtbxemail.Focus();
auto_load_table();
}
else
{
myrdr = cmnd.ExecuteReader();
ExecMyQuery(cmnd, "You are trying to update a record");
auto_load_table();
View_all_data disvid = new View_all_data();
this.Dispose();
disvid.Show();
while (myrdr.Read())
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
myConn.Close();
}