October 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

ADO.NET Overview

  • November 19, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

Sample access using Data Reader:

       ' Setup connection

       Dim myConnection AsNew 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 AsNew OleDb.OleDbCommand( _
         "Select * from Customers", myConnection)

       Dim myReader AsOleDb.OleDbDataReader 

       ' Open connection

       myConnection.Open()

       ' Execute and put results into reader

       myReader = myCommand.ExecuteReader

       ' Read through all the records

       Do UntilmyReader.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 AsNew 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 AsNew OleDb.OleDbDataAdapter( _
         "Select * from Customers", myConnection) 

       Dim myDataSet AsNew 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 somethinglike:

       '   myDataAdapter.Update(myDataSet) 

 

Sample access using XML:

       Dim myDataSet AsNew 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")





Page 7 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel