Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to perform ADO.NET connection to MS Access database, place into dataset and manipulate

Posted on 2010-08-17
8
Medium Priority
?
333 Views
Last Modified: 2012-05-10
Hi I've been unwittingly using ADO without realising there was a difference between that and ADO.NET.

Anyway, I just want to try and switch to ADO.NET but i'm not really sure how to do it. I've gotten as far as below... made the connection to the database and everything went fine there. What do I do from here? I guess i need to put the data into a dataset and start working with it... how do i go about this? If I have a int column called 'Int1' and a string column called 'string1' for example, how do i go about looping through, extracting data etc...

Now, I've seen a second piece of code about this which i put at the end below, which seems to show how to create a dataset... although what confuses me here is that it seems to be creating it's own connection to the database? Some help please!
'Connection to database
Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & "'" & Server.MapPath("~/App_Data/CTC Database.mdb") & "'"
Dim cnnRead As OleDbDataReader
 
      strSQL = "SELECT * FROM([CTC Table])"

With cnn
         .Connection = New OleDbConnection(strConnection)
         .Connection.Open()
         .CommandText = strSQL
         cnnRead = .ExecuteReader()
End With

'Second piece of code i've seen to create a dataset
Dim da As New OleDbDataAdapter(strSQL, strConnection)
Dim ds As New DataSet()
da.Fill(ds, "Customers")

Open in new window

0
Comment
Question by:AidenA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 4

Expert Comment

by:justin-clarke
ID: 33455732
If you use the above code you can loop through the dataset like so....


If (ds.Tables(0).Rows.Count > 0) Then

   For Each row As DataRow In ds.Tables(0).Rows

      'inside here you can access all the "rows"
      Dim strInt1 As String = row("Int1").ToString()

      Dim strString1 As String = row("String1").ToString()

   Next

End If

Open in new window

0
 

Author Comment

by:AidenA
ID: 33455787
Hi, thanks,

but as in my question... i'm just wondering also, how to get from first piece of code to second piece of code. What should i be using here... this datareader or OleDbDataAdapter? can you just link it together for me as i'm having trouble figuring it out...
0
 
LVL 4

Accepted Solution

by:
justin-clarke earned 1600 total points
ID: 33455916
Sorry I see what you mean.
Dim cn As New System.Data.OleDb.OleDbConnection
Dim cm As New System.Data.OleDb.OleDbCommand
Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & "'" & Server.MapPath("~/App_Data/CTC Database.mdb") & "'"

cn.ConnectionString = strConnection
cm.Connection = cn
cn.Open()
				
Dim strSQL As String = "SELECT * FROM tblTable"		

Dim da As New OleDbDataAdapter(strSQL, strConnection)
Dim ds As New DataSet()
da.Fill(ds)


'loop through the rows here


cn.Close()

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:justin-clarke
ID: 33455926
Think that should be OK off the top of my head.
0
 

Author Comment

by:AidenA
ID: 33456174
yeah seemed to work ok thanks.

so just one last thing then... what is cnnRead = .ExecuteReader() used for then in the above code?
0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 400 total points
ID: 33456938
hi
for this question read this helpfull article http://blogs.x2line.com/al/archive/2007/05/01/3049.aspx
0
 

Author Comment

by:AidenA
ID: 33457090
ok thanks, so i should use in certain situations. Can't get the dataview to work though. Below is what i have... i put into a class (is that the right way to go about it?)

but doesn't work cmd.ExecuteReader throws an exception because it says 'Object reference not set to an instance of an object.' How do i create this details view properly?

'In one of my webpages where i want to create a connection

Dim objCC As New CreateConnection(strConnection, strSQL)
objCC.CreateDataView("Ref No")

'class to create connection
Public Class CreateConnection

   Public ds As New DataSet()
   Public cnnADO As OleDbConnection
   Public cmd As OleDbCommand
   Public dt As DataTable

   Public Sub New(ByVal strConnection As String, ByVal strSQL As String)

      cnnADO = New OleDbConnection(strConnection)
      cmd = New OleDbCommand(strConnection, cnnADO)
      cmd.CommandText = strSQL

      cnnADO.Open()
      Dim da As New OleDbDataAdapter(strSQL, strConnection)
      da.Fill(ds)
      
   End Sub
   Public Sub CreateDataView(ByVal strColumn)
      dt = New DataTable()
      dt = ds.Tables("CTC Table")
      dt.Load(cmd.ExecuteReader())
      Dim dv As DataView = New DataView(dt)
      dv.Sort = strColumn
   End Sub
End Class

Open in new window

0
 
LVL 4

Expert Comment

by:justin-clarke
ID: 33464205
Hi

I think you are mixing two ways of "filling" your data table.

If you take out the line:

dt.Load(cmd.ExecuteReader())

It should be OK.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

610 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