DataReader的未关闭时,连接被关闭,后果是什么?后果、DataReader

2023-09-03 20:35:39 作者:梦境

例如我有这个code:

for example i have this code :

Sub Month()
    Dim Conn As New Data.OracleClient.OracleConnection
    Conn.Open()
    Try

        Dim Cmd As New Data.OracleClient.OracleCommand
        With Cmd
            .Connection = Conn
            .CommandType = Data.CommandType.Text
            .CommandText = "SELECT * FROM MONTH"
        End With
        Dim datareader As Data.OracleClient.OracleDataReader = Cmd.ExecuteReader
        While datareader.Read
            Response.Write(datareader(0))
        End While
    Catch ex As Exception
        Throw ex
    Finally
        Conn.Close()
    End Try
End Sub

会发生什么DataReader的时候连接关闭(Conn.close)

What will happen to the datareader when the Connection is closed ( Conn.close)

将所使用DataReader的光标被释放?还是会继续开放?

Will the Cursor that is used by the datareader be freed ? or will it stay open ?

如果所使用DataReader的光标仍处于打开状态时,它会自动关闭?或者我应该只是手动关闭它?

If the cursor that is used by the datareader is still open , when will it be automatically closed ? or should i just closed it manually ?

它会导致可怕的ORA-01000:最大打开游标数?

Will it cause the dreaded "ORA-01000: maximum open cursors exceeded" ?

在此先感谢

推荐答案

只要数据读取的新对象,它被关闭后,

Just make new object of data reader after it been closed

private void button2_Click(object sender, EventArgs e)
    {
        //SqlConnection cn1 = new SqlConnection();
        cn.ConnectionString = "server = .\\SQLEXPRESS ; database=store ; integrated security = true  ";
        SqlCommand cm = new SqlCommand("select * from emp", cn);
        cn.Open();
        SqlDataReader dr = cm.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        dataGridView1.DataSource = dt.DefaultView ;
        //SqlCommand cm3 = new SqlCommand("select * from emp", cn1);
        SqlDataReader dr1 = cm.ExecuteReader();
        listBox1.Items.Clear();
        while (dr1.Read())
        {
            //listBox1.Items.Add(dr.GetString(2));
            listBox1.Items.Add(dr1["name"]);

        }
        cn.Close();
    }