Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Return an ADO.NET Result Set from a function

Posted on 2006-12-01
8
Medium Priority
?
292 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

927 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