为什么我不能在一种方法中调用 OpenConnection() 两次?当我调用它时,会出现此错误:
连接已经打开。
SelectDisPatient()
我在和中调用它两次Count()
。请参阅我的代码for (int index = 0; index < Count(); index++)
这种方法SelectDisPatient
:
public void SelectDisPatient(FrmVIRGO frm)
{
string query = "SELECT id_pasien FROM tb_patient_information ";
if (this.OpenConnection() == true)
{ //Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dataReader.HasRows)
{
for (int index = 0; index < Count(); index++)
dataReader.Read();
frm.tbName.Text = dataReader[0].ToString();
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
}
}
这种Count()
方法:
public int Count()
{
string query = "SELECT Count(*) FROM tb_patient_information";
int Count = -1;
//Open Connection
if (this.OpenConnection() == true)
{
//Create Mysql Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//ExecuteScalar will return one value
Count = int.Parse(cmd.ExecuteScalar()+"");
//close Connection
this.CloseConnection();
return Count;
}
else
{
return Count;
}
}
(this.OpenConnection() == true)
但是当我在方法中删除时,Count()
它说我需要关闭连接。