Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Database
Database General Discussion
Return rows from sqlite query function
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="jmcilhinney, post: 186154, member: 641"] You have declared a function that returns a data reader but your code doesn't actually return one. One option would be to not close the connection and open your data reader with the appropriate [ICODE]CommandBehavior[/ICODE] so that the connection is closed when the reader is closed. You then rely on the consumer to use and close the reader, e.g. [CODE=vbnet]Public Function GetDataReader(query As String) As SqlDataReader Dim connection As New SqlConnection("connection string here") Dim command As New SqlCommand(query, connection) connection.Open() Return command.ExecuteReader(CommandBehavior.CloseConnection) End Function[/CODE] and then: [CODE=vbnet]Using reader = GetDataReader("SELECT * FROM Table1") While reader.Read() '... End While End Using[/CODE] Alternatively, you can return a [ICODE]DataTable[/ICODE] instead: [CODE=vbnet]Public Function GetDataTable(query As String) As DataTable Using connection As New SqlConnection("connection string here"), command As New SqlCommand(query, connection) connection.Open() Dim table As New DataTable Using reader = command.ExecuteReader(CommandBehavior.CloseConnection) table.Load(reader) End Using Return table End Using End Function[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Database
Database General Discussion
Return rows from sqlite query function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom