Solved

Return an ADO.NET Result Set from a function

Posted on 2006-12-01
8
260 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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