Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 11th, 2005, 12:03 PM
Authorized User
 
Join Date: Feb 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default code to read in Access tables to a dataset - hlp!

Hi.

I'm completely new to VB.Net and v. limited in VB!

Can someone point give me a simple piece of code to read data from several tables in a db called SUBMAN into a Dataset and transfer that to a textbox?

thanks in advance.

Here's as far as I've got!

    Private Sub btnCountry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCountry.Click


        Dim DS As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim MyConnection As System.Data.OleDb.OleDbConnection

        ' connect to the database
        MyConnection = New OleDbConnection( _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & "c:\DB\SUBMAN.mdb" & ";")
        MyConnection.Open()

        Dim sqlstring As String
        ' create the sqlstring ---- how do I get the command to use it?

        sqlstring = "SELECT title, firstname, lastname, email, town, postcode, country"
        sqlstring &= " FROM SUBSCRIBERS, ADDRESSES WHERE country = " & "'" & ComboCountry.Text & "'"

        DS = New System.Data.DataSet()
        MyCommand.Fill(DS)
        MyConnection.Close()

        'process each row

        ' Clear the variable

        'get the data in each column

        ' remove trailing coma


        ' Write the data to the text box
        txtResults.Text &= ' datafrom dataset

        ' close the conection and clean up
        MyConnection.Close()
        MyConnection.Dispose()
        MyConnection = Nothing
        DS.Dispose()
        DS = Nothing
        MyCommand.Dispose()
        MyCommand = Nothing

    End Sub
 
Old February 11th, 2005, 02:18 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote: MyConnection.Dispose()
        MyConnection = Nothing
        DS.Dispose()
        DS = Nothing
        MyCommand.Dispose()
        MyCommand = Nothing

1-why you had such lines in your code,remember that in .NET all such task accomplished by Garbage Collector
2-for DataSet there is no need for opening the connection(Fill method would open and close the connection),
Code:
Dim dstAuthors As DataSet
Dim conAuthors As OleDbConnection
Dim dadAuthors As OleDbDataAdapter

dstAuthors = New DataSet()
conAuthors = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=c:\Authors.mdb" )
dadAuthors = New OleDbDataAdapter( "Select * From Authors", conAuthors )
dadAuthors.Fill( dstAuthors, "Authors" )
then you can work on the DataSet and go on ..

_____________
Mehdi.
software student.
 
Old February 11th, 2005, 02:34 PM
Authorized User
 
Join Date: Feb 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mehdi,

thanks for the tips, can you help again?

How do I transfer the contents of the dataset DS to the text box txtResults?

I hope you can help again, thanks again,

Ru.
 
Old February 11th, 2005, 06:10 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you can get every cell in your DataSet,
Code:
CType(DS.Tables([TableNumber or TableName]).Rows([RowNumber])([ColumnNumber or ColumnName]),String)
look into MSDN.

_____________
Mehdi.
software student.
 
Old February 12th, 2005, 06:03 PM
Authorized User
 
Join Date: Feb 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again Mehdi,

you're magic! I'll just go and try all that now!

Ru.





Similar Threads
Thread Thread Starter Forum Replies Last Post
read nested xml into a dataset sansircar ASP.NET 1.0 and 1.1 Professional 0 August 1st, 2006 03:08 PM
Read XML to DataSet with XPath? striker9 ASP.NET 2.0 Basics 1 July 25th, 2006 03:30 AM
Read Linked tables in Access from VB code meggan VB Databases Basics 3 March 4th, 2005 11:26 AM
Re: SQL Server dataset to ACCESS dataset dazzer ADO.NET 0 March 22nd, 2004 05:28 AM
Want to code external linked tables in Access Michael Allaire Access VBA 1 July 23rd, 2003 06:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.