Closing DataReader ? : Releasing Memory

A

Arsalan

I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSettings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()


But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the Page_Load
method then everything works fine, but problem occurs when this function is
in external class. the database doesnt close]
 
S

Sharon

Hi Arsalan.
I think it's a bad idea to return DataReader.
That is because the DataReader holds the connection open,
and you depend on the caller to close it.
I suggest you make a function that returns a DataView.
Sharon.
 
A

Arsalan

Good idea buddy, but you cannot close the connection and read data from
DataReader [please see the code], and as connection object is out of scope,
you cannot close it through Page_Load event.
Eliyahu Goldin said:
Close the connection:

conn.Close();

Eliyahu

Arsalan said:
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSettings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()


But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this function is
in external class. the database doesnt close]
 
A

Arsalan

Thanks,
Sharon said:
Hi Arsalan.
I think it's a bad idea to return DataReader.
That is because the DataReader holds the connection open,
and you depend on the caller to close it.
I suggest you make a function that returns a DataView.
Sharon.

Arsalan said:
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSettings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()


But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this function is
in external class. the database doesnt close]
 
E

Eliyahu Goldin

Take connection object out of that function scope.

Eliyahu

Arsalan said:
Good idea buddy, but you cannot close the connection and read data from
DataReader [please see the code], and as connection object is out of scope,
you cannot close it through Page_Load event.
Eliyahu Goldin said:
Close the connection:

conn.Close();

Eliyahu

Arsalan said:
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSettings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()


But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this
function
is
in external class. the database doesnt close]
 
A

Arsalan

Good suggestion but coding everything would be tedious, is there any
alternate way to do it ??
Eliyahu Goldin said:
Take connection object out of that function scope.

Eliyahu

Arsalan said:
Good idea buddy, but you cannot close the connection and read data from
DataReader [please see the code], and as connection object is out of scope,
you cannot close it through Page_Load event.
Eliyahu Goldin said:
Close the connection:

conn.Close();

Eliyahu

I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSettings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()


But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this function
is
in external class. the database doesnt close]
 
R

Rob Nicholson

I think it's a bad idea to return DataReader.
That is because the DataReader holds the connection open,

That depends upon his application really. Nothing wrong as such as long as,
as you say, it's closed correctly.

DataReader's are supposed to be fast but for small data sets, I've not found
much difference between a DataReader and a DataSet,

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top