Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Return an ADO.NET Result Set from a function

Posted on 2006-12-01
8
Medium Priority
?
286 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 400 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 1280 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 320 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 1280 total points
ID: 18054045
iffy it is
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 320 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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