Solved

Return an ADO.NET Result Set from a function

Posted on 2006-12-01
8
277 Views
Last Modified: 2012-06-21
I have done this previously using DAO, ADO and JDBC but am befudged by ADO.NET.

I want to send an SQL string into a function and get it to return a Result Set (would accept a string array). I just don't have any more time to play around with this and would appreciate any help.
0
Comment
Question by:derekclee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 100 total points
ID: 18052923
Hi derekclee,

Private Function GetReader(ByVal Parameters() As String) As SqlClient.SqlDataReader
    Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.SqlClient.SqlCommand
    Dim reader As System.Data.SqlClient.SqlDataReader
    With sqlCommand
        .Connection = New System.Data.SqlClient.SqlConnection("Your connection string here")
        .Connection.Open
        .CommandText="Your command text here"
        .CommandType=CommandType.Text
        reader = .ExecuteReader(CommandBehaviour.CloseConnection)
    End With
    Return reader
End Function

This is just the bare bones of how to achieve this. The key point is the use of commandbehaviour.closeconnection when opening the datareader as this will close the connection when the data reader is closed in the calling procedure.

Tim Cottee
0
 

Author Comment

by:derekclee
ID: 18053156
Hi Tim,

I get a "Unknown connection option in connection string: provider." error. I believe the string to be correct but should point out that I am using an Access 2002 table at the back end. Is your code specific to SQL Server?

Cheers,
Derek
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 320 total points
ID: 18053239
yes that code is specific to sql server. to make it for access you should use the OleDbClient. also, you said you need a recordset, which is now called Dataset on ADO.NET. this function would do the trick i think:

Private Function GetDataset(ByVal Parameters() As String) As System.Data.DataSet

        Dim MyQuery As String = "SELECT * FROM MyTable"
        Dim MyConn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;")
        Dim MyCommand As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand(MyQuery, MyConn)

        Dim MyDataset As System.Data.DataSet = Nothing
        Dim MyAdapter As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(MyCommand)

        MyConn.Open()
        MyAdapter.Fill(MyDataset)
        MyConn.Close()

        Return MyDataset

    End Function

NY


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:derekclee
ID: 18053475
getting closer but I get the following error: "Value cannot be null" (MyAdapter.Fill(MyDataset)). It would seem that the

Code as follows:

strSQL = "SELECT Name FROM Services_Data ORDER BY Name" (this is sent in from the calling method).

Public Class getData

    Private connectionString As String

    Sub New()

        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\derek\Desktop\VBasic\Services_Database.mdb;" & _
        "User Id=admin;Password=;"
       
    End Sub

    Public Function GetDataset(ByVal strSQL As String) As System.Data.DataSet

        Dim MyConn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
        Dim MyCommand As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand(strSQL, MyConn)

        Dim MyDataset As System.Data.DataSet
        Dim MyAdapter As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(MyCommand)

        MyConn.Open()
        MyAdapter.Fill(MyDataset)
        MyConn.Close()

        Return MyDataset

    End Function

End Class

I have tested the SQL string within the Access database and know that it is correct. Any ideas?

Thanks.
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 80 total points
ID: 18054001
This looks iffy to me

        Dim MyDataset As System.Data.DataSet

I think it should be

        Dim MyDataset As New System.Data.DataSet

Roger
0
 
LVL 13

Assisted Solution

by:newyuppie
newyuppie earned 320 total points
ID: 18054045
iffy it is
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 80 total points
ID: 18054086
One other point.  Name is a reserved word in Access.  If you now get an OleDb error reported, change

strSQL = "SELECT Name FROM Services_Data ORDER BY Name"

to

strSQL = "SELECT [Name] FROM Services_Data ORDER BY [Name]"

Roger
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18054232
Minor point... recordset => datatable, not dataset...
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question