Diff between Dataset and DataTable

korae

Member
Joined
Jan 24, 2010
Messages
22
Programming Experience
Beginner
I have this code that fills my datagrid with data:

VB.NET:
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Network_Info.mdb"
            Dim myConnection As OleDbConnection = New OleDbConnection
            myConnection.ConnectionString = connString
            ' create a data adapter
            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select DateRecorded As [Date Recorded], Format([TimeRecorded],'HH:MM:SS AM/PM') As [Time Recorded], Computer_Name As [Computer Name], Fastest_Download_Speed & ' Kb/s' As [Fastest Download Speed] , Fastest_Upload_Speed & ' Kb/s' As [Fastest Upload Speed], Average_Download_Speed & ' Kb/s' As [Average Download Speed], Average_Upload_Speed & ' Kb/s' As [Average Upload Speed], Total_Downloads & ' KB' As [Total Downloads], Total_Uploads & ' KB' As [Total Uploads], IP_Address, ElapsedTime As [Elapsed Time] from Computer_Connection_Info Order by DateRecorded, TimeRecorded", myConnection)

            Dim ds As DataSet = New DataSet

            da.Fill(ds, "Computer_Connection_Info")

            DataGrid1.DataSource = ds.DefaultViewManager

I notice I'm using a dataset right? Then how am I suppose to fill my datagrid with data using a datatable? I've been very confused bout the topics in web like use a datatable or use a dataset bla bla bla...
 
•◘•

From MSDN:
MSDN said:
The DataSet, which is an in-memory cache of data retrieved from a data source, is a major component of the ADO.NET architecture. The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects. You can also enforce data integrity in the DataSet by using the UniqueConstraint and ForeignKeyConstraint objects. For further details about working with DataSet objects, see DataSets, DataTables, and DataViews (ADO.NET).

Whereas DataTable objects contain the data, the DataRelationCollection allows you to navigate though the table hierarchy. The tables are contained in a DataTableCollection accessed through the Tables property.
 
I have this code that fills my datagrid with data:

Read the DW3 link in my signature, section Creating a Simple Data App

-> this code you have written is better off written for you by the IDE, leaving you to get on with more important things like writing the app. Read the tutorial above, it will make your life much easier (and make your code better!)
 
A DataTable is similar to a table in your database. A DataSet is similar to the actual database where it can hold many tables within it. A DataGridView displays your data from your DataTable however you are setting the whole DataSet as the DataSource and it doesnt know which DataTable to display. You need to specify the DataTable within the DataSet.

VB.NET:
Dim ds As DataSet = New DataSet

da.Fill(ds, "Computer_Connection_Info")
DataGrid1.DataSource = ds[COLOR="Red"].Tables("Computer_Connection_Info")[/COLOR]

Or
VB.NET:
Dim ds As DataSet = New DataSet

da.Fill(ds, "Computer_Connection_Info")

[COLOR="SeaGreen"]'Specify by table index within the dataset[/COLOR]
DataGrid1.DataSource = ds[COLOR="Red"].Tables(0)[/COLOR]
 
Back
Top