S
Siv
Cor,
This conversation you are having with Jack raises a question about disposal
which I have never been fully happy about. I often create functions that
return say a datareader to the caller. The purpose being to have a nice
little black box that does the job of getting data from a database based on
the paramters you send it.
My function would look something like this:
Private Function GetData(ByVal ID as integer) as SQLDatareader
Dim DR as SQLDataReader=nothing
Dim Cmd as SQLCommand=Nothing
Dim Cn as SQLConnection= Nothing
Dim strSQL as string = ""
Try
strSQl = "Select * From SomeTable where fkSomeForeignKey=" &
ID.Tostring & " ORDER BY SomeField;"
Cn = New SqlConnection(ConnString)
Cn.Open()
Cmd = New SqlCommand(strSQL, Cn)
Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return Dr
Catch ex As Exception
PEH("GetAllRecords", "Documents Class", ex.Message)
Return Dr
End Try
End Function
What I would really like to do is have a Finally block that disposes od Dr,
but if I do that I will get an error when I try and read the returned
DataReade in the calling routine as the Finally call will remove the version
I have sent to the caller before it gets it.
Is just leaving the function like that OK, or is there some way you can send
a copy of the datareader back to the caller and dispose the one in the body
of the function without causing problems?
Siv
This conversation you are having with Jack raises a question about disposal
which I have never been fully happy about. I often create functions that
return say a datareader to the caller. The purpose being to have a nice
little black box that does the job of getting data from a database based on
the paramters you send it.
My function would look something like this:
Private Function GetData(ByVal ID as integer) as SQLDatareader
Dim DR as SQLDataReader=nothing
Dim Cmd as SQLCommand=Nothing
Dim Cn as SQLConnection= Nothing
Dim strSQL as string = ""
Try
strSQl = "Select * From SomeTable where fkSomeForeignKey=" &
ID.Tostring & " ORDER BY SomeField;"
Cn = New SqlConnection(ConnString)
Cn.Open()
Cmd = New SqlCommand(strSQL, Cn)
Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return Dr
Catch ex As Exception
PEH("GetAllRecords", "Documents Class", ex.Message)
Return Dr
End Try
End Function
What I would really like to do is have a Finally block that disposes od Dr,
but if I do that I will get an error when I try and read the returned
DataReade in the calling routine as the Finally call will remove the version
I have sent to the caller before it gets it.
Is just leaving the function like that OK, or is there some way you can send
a copy of the datareader back to the caller and dispose the one in the body
of the function without causing problems?
Siv