我目前正在完成我的工资单系统,其中ms访问数据库用于连接我的程序。 CRUD是我们完成这个项目的主要要求之一,但是ADD按钮表示我的连接没有关闭。 Connection的当前状态是开放的。
private void btnAddDB_Click(object sender, EventArgs e)
{
try
{
Connection.Open();
OleDbCommand command1 = new OleDbCommand();
OleDbCommand command2 = new OleDbCommand();
OleDbCommand command3 = new OleDbCommand();
command1.Connection = Connection;
command2.Connection = Connection;
command3.Connection = Connection;
command1.CommandText = "Select* from employeeinfo where Employee ID = '" + txtEID.Text + "'";
OleDbDataReader reader1 = command1.ExecuteReader();
command2.CommandText = "Select* from employeeinfo where Email Address = '" + txtEA.Text + "'";
OleDbDataReader reader2 = command2.ExecuteReader();
command3.CommandText = "Select* from employeeinfo where Contact Number = '" + txtCN.Text + "'";
OleDbDataReader reader3 = command3.ExecuteReader();
string conn1, conn2, conn3;
while (reader1.Read())
{
con1.Text = reader1["Employee ID"].ToString();
}
while (reader2.Read())
{
con2.Text = reader2["Email Address"].ToString();
}
while (reader3.Read())
{
con3.Text = reader3["Contact Number"].ToString();
}
conn1 = con1.Text;
if (conn1 != con1.Text)
{
conn2 = con2.Text;
if (conn2 != con2.Text)
{
conn3 = con3.Text;
if (conn3 != con3.Text)
{
OleDbCommand command = new OleDbCommand();
command.Connection = Connection;
command.CommandText = "INSERT INTO employeeinfo ([Employee ID], [Employee Name], [Department], [First Name], [Middle Name, [Surname], [Address], [Contact Number], [Email Address], [Salary]) values(@empid, @empname, @dep, @fname, @mname, @sname, @address, @connum, @emadd, @sal)";
command.Parameters.AddWithValue("@empid", txtEID.Text);
command.Parameters.AddWithValue("@empname", txtEN.Text);
command.Parameters.AddWithValue("@dep", txtDep.Text);
command.Parameters.AddWithValue("@fname", txtFN.Text);
command.Parameters.AddWithValue("@mname", txtMN.Text);
command.Parameters.AddWithValue("@sname", txtSN.Text);
command.Parameters.AddWithValue("@address", txtAdd.Text);
command.Parameters.AddWithValue("@connum", txtCN.Text);
command.Parameters.AddWithValue("@emadd", txtEA.Text);
command.Parameters.AddWithValue("@sal", textBox2.Text);
command.ExecuteNonQuery();
MessageBox.Show("Employee Information Created");
}
else if (conn3 == txtCN.Text)
{
MessageBox.Show("Contact Number Already exist");
con3.Text = "";
}
}
else if (conn2 == txtEA.Text)
{
MessageBox.Show("Email Address Already exist");
con2.Text = "";
}
}
else if (conn1 == txtEID.Text)
{
MessageBox.Show("Email ID Already exist");
con1.Text = "";
}
Connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
}