ADO.NET Overview
Sample access using Data Reader:
' Setup connection
Dim myConnection As
New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Visual " & _
"Studio.NET\Common7\Tools\Bin\nwind.Mdb")
' Setup command object, specifying SELECT
' and connection to use
Dim myCommand As
New OleDb.OleDbCommand( _
"Select * from Customers", myConnection)
Dim myReader As
OleDb.OleDbDataReader
' Open connection
myConnection.Open()
' Execute and put results into reader
myReader = myCommand.ExecuteReader
' Read through all the records
Do Until
myReader.Read = False
MessageBox.Show(myReader("CompanyName"))
Loop
' Close reader connection before continuing
myReader.Close()
myConnection.Close()
Sample access using Data Adapter:
' Setup connection
Dim myConnection As
New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Visual " & _
"Studio.NET\Common7\Tools\Bin\nwind.Mdb")
' We're just specifying the SELECT here.
' If done visually using the Server Explorer,
' the INSERT/UPDATE/DELETE + Parameters
' collection would be auto-specified for us
Dim myDataAdapter As
New OleDb.OleDbDataAdapter( _
"Select * from Customers", myConnection)
Dim myDataSet As
New DataSet()
' Fill DataSet with table, call it 'Customers'
myDataAdapter.Fill(myDataSet, "Customers")
' Display first CompanyName field of first
' row in Customers table
MessageBox.Show(myDataSet.Tables("Customers").Rows(0)("CompanyName"))
' If we had specified INSERT/UPDATE/DELETE
' commands + Parameters collection, we could
' also now edit the data, then run something
like:
'
myDataAdapter.Update(myDataSet)
Sample access using XML:
Dim myDataSet As
New DataSet()
myDataSet.ReadXml("c:\books.xml")
' Books.xml is file bundled with VS.NET,
' typically located at:
' C:\Program Files\Microsoft.NET\FrameworkSDK
' ... \Samples\quickstart\howto\samples\xml
' ... \xmldocumentevent\vb\books.xml
' Counts the number of book nodes
MessageBox.Show(myDataSet.Tables("book").Rows.Count)
' Retrieves the last name of the first author
MessageBox.Show(myDataSet.Tables( _
"author").Rows(0)("last-name"))
' Updates the last name
myDataSet.Tables("author").Rows(0)( _
"last-name") = "Flandadenham"
' Rewrites the XML file
myDataSet.WriteXml("c:\books.xml")
