Solved

Return an ADO.NET Result Set from a function

Posted on 2006-12-01
8
238 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:derekclee
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 80 total points
Comment Utility
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
Comment Utility
iffy it is
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 80 total points
Comment Utility
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
Comment Utility
Minor point... recordset => datatable, not dataset...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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 …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now