C#中连接未关闭

来源:百度知道 编辑:UC知道 时间:2024/09/22 07:15:02
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
dataGrid1.DataSource = null;
dataGrid1.CaptionText = "";
if (comboBox2.DataSource != null)
{
connection1.Open();
string sql = "select year(enrolmenttime) from classinfo where classid = " + comboBox2.SelectedValue;
OleDbCommand cmd = new OleDbCommand(sql, connection1);
int rxnf = Int32.Parse(cmd.ExecuteScalar().ToString());
sql = "select length from classinfo where classid = " + comboBox2.SelectedValue.ToString().Trim();
cmd.CommandText = sql;
int nianzhi = Int32.Parse(cmd.ExecuteScalar().ToString());
int nf = rxnf;
comboBox3.Items.Clear();
for (int i = 1; i <= nianzhi * 2; i++)

connection1.Open();
改成

if(connection1.State != ConnectionState.Open)
{connection1.Open();}

数据库连接是比较宝贵的资源一定要处理好

dr.close();
connection1.close();

dr没关闭

用完dr,没有关闭哦。
dr.close()