Connection关闭时DataReader没有关闭,后果如何?

发布于 2024-11-10 13:49:29 字数 797 浏览 3 评论 0原文

例如我有这样的代码:

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

当连接关闭(Conn.close)时数据读取器会发生什么

数据读取器使用的游标会被释放吗?或者它会保持开放吗?

如果datareader使用的游标仍然打开,什么时候它会自动关闭?或者我应该手动关闭它?

它会导致可怕的“ORA-01000:超出最大打开游标数”吗?

提前致谢

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

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

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

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 ?

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

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她比我温柔 2024-11-17 13:49:29

您应该在 using 块中创建对象,以便正确处置它们:

Using Conn As New Data.SqlClient.SqlConnection
    Conn.Open()

    Dim Cmd As New Data.SqlClient.SqlCommand
    With Cmd
        .Connection = Conn
        .CommandType = Data.CommandType.Text
        .CommandText = "SELECT * FROM MONTH"
    End With

    Using datareader As Data.SqlClient.SqlDataReader = Cmd.ExecuteReader()
        While datareader.Read()
            Response.Write(datareader(0))
        End While
    End Using
End Using

无需在连接或数据读取器上调用 Close。

You should create the objects in a using block so they are properly disposed:

Using Conn As New Data.SqlClient.SqlConnection
    Conn.Open()

    Dim Cmd As New Data.SqlClient.SqlCommand
    With Cmd
        .Connection = Conn
        .CommandType = Data.CommandType.Text
        .CommandText = "SELECT * FROM MONTH"
    End With

    Using datareader As Data.SqlClient.SqlDataReader = Cmd.ExecuteReader()
        While datareader.Read()
            Response.Write(datareader(0))
        End While
    End Using
End Using

There is no need to call Close on either the connection or the datareader.

转瞬即逝 2024-11-17 13:49:29
CommandBehavior.CloseConnection

当您将以上值作为参数传递给 ExecuteReader 时
1.不需要显式关闭连接,当您关闭阅读器时,连接会关闭,

检查全文:http://pranayamr.blogspot.com/2010/11/executereader-with-commanbehavior.html

CommandBehavior.CloseConnection

When you pass above values as argument to ExecuteReader
1. there is no need to close connection explicitly connection get close when you close your reader

check full post : http://pranayamr.blogspot.com/2010/11/executereader-with-commanbehavior.html

疑心病 2024-11-17 13:49:29

只需在关闭后创建数据读取器的新对象

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();
    }

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();
    }
夜声 2024-11-17 13:49:29

为什么你不应该像这样明确地关闭阅读器。

datareader.Close()

Dim Conn As New Data.SqlClient.SqlConnection
Conn.Open()
Try

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

Why shouldn't you explicitly close the reader like this.

datareader.Close()

Dim Conn As New Data.SqlClient.SqlConnection
Conn.Open()
Try

    Dim Cmd As New Data.SqlClient.SqlCommand
    With Cmd
        .Connection = Conn
        .CommandType = Data.CommandType.Text
        .CommandText = "SELECT * FROM MONTH"
    End With
    Dim datareader As Data.SqlClient.SqlDataReader = Cmd.ExecuteReader
    While datareader.Read
        Response.Write(datareader(0))
    End While
    datareader.Close()
Catch ex As Exception
    Throw ex
Finally
    Conn.Close()
End Try
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文