Recordsets in .NET

  • Thread starter Thread starter CR
  • Start date Start date
C

CR

I am making the switch to .NET from VB6. In VB6 you could access data
by using the Data Environment and Data Control. I found both to be
totally useless. I can just as easily accomplish the same task with
code. For example if I wanted to populate a textbox I would do
something like this:

Dim rst As New Recordset
Dim cnn as New Connection

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\test\test.mdb;"
rst.Open "Select * From Table1", cnn
Text1.Text = rst!Field1

Using code, in my opinion, is much easier and more flexible than using
the Data Environment Wizard and the data control. Plus you can use in
in your VBA macros.

I have a tutorial on .NET but it seems to only cover .NET's version of
the Data Environment and Data control. I think they call it the Server
Explorer and Data Adapter.

Is there a way to access data using straight VB code like I did in
VB6?

Thanks!

Chuck.
 
You can do the same in .NET (which I prefer over those
drag and drop wizards):

Dim cnn As OleDbConnection = New OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\test\test.mdb;")
cnn.Open()
Dim cmd As OleDbCommand = New OleDbCommand("Select * From
Table1", cnn)
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
If rdr.Read() Then
Text1.Text = rdr(0)
End If

Bin Song, MCP
 
Hi CR,

In addition to Bing Song a little sample, how to use it with a dataset
Just typed from your example, so watch typos.

\\\\
dim connString as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\test\test.mdb;"
dim SqlString as string = "Select * From Table1"
Dim conn As New OledbConnection(connString)
Dim cmd As New OleDbCommand(sqlStr, Conn)
Dim ds As New DataSet
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, "Table1")
conn.close
///

I hope this helps a little bit.

Cor
 
Thanks! That saves me a lot of trouble.

You can do the same in .NET (which I prefer over those
drag and drop wizards):

Dim cnn As OleDbConnection = New OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\test\test.mdb;")
cnn.Open()
Dim cmd As OleDbCommand = New OleDbCommand("Select * From
Table1", cnn)
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
If rdr.Read() Then
Text1.Text = rdr(0)
End If

Bin Song, MCP
 
I appreciate the help!

Chuck.

Hi CR,

In addition to Bing Song a little sample, how to use it with a dataset
Just typed from your example, so watch typos.

\\\\
dim SqlString as string = "Select * From Table1"
Dim conn As New OledbConnection(connString)
Dim cmd As New OleDbCommand(sqlStr, Conn)
Dim ds As New DataSet
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, "Table1")
conn.close
///

I hope this helps a little bit.

Cor
 
Cor said:
Hi CR,

In addition to Bing Song a little sample, how to use it with a dataset
Just typed from your example, so watch typos.

\\\\
dim SqlString as string = "Select * From Table1"
Dim conn As New OledbConnection(connString)
Dim cmd As New OleDbCommand(sqlStr, Conn)
Dim ds As New DataSet
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, "Table1")
conn.close

I figured out how to access data in the DataReader but I can't figure
out how to access the data in the DataSet. In this example if I had a
field in Table1 called Field1, how would I get to the value in Field1?
 
CR said:
I figured out how to access data in the DataReader but I can't
figure out how to access the data in the DataSet. In this example if
I had a field in Table1 called Field1, how would I get to the value
in Field1?

Only a question: Have you already read the documentation of the DataSet (and
other ADO.NET related topics)?
 
I figured out how to access data in the DataReader but I can't figure
out how to access the data in the DataSet. In this example if I had a
field in Table1 called Field1, how would I get to the value in Field1?

For the first record(row)

ds.tables(0).rows(0).item("Field1")

"> > \\\\
 
Armin Zingler said:
Only a question: Have you already read the documentation of the DataSet (and
other ADO.NET related topics)?

Some of the online help, but it's a little overwhelming. For example
I'm not sure if I should even be using the DataSet object, maybe I
only need the DataReader. What documentation would you recommend I
read to get a good overview of this topic?
 
CR said:
Some of the online help, but it's a little overwhelming. For
example I'm not sure if I should even be using the DataSet object,
maybe I only need the DataReader. What documentation would you
recommend I read to get a good overview of this topic?

Concerning the Dataset I recommend
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingusingdatasets.asp
http://msdn.microsoft.com/library/en-us/vbcon/html/vboriDatasets.asp

If you only want to process data from the database sequentially, you can use
a DataReader. As the name says, it is used to read the data from the
databse. A dataset is an object that locally stores the content of the
records read, and you'll be able to access the records randomly.
 
Armin Zingler said:
Concerning the Dataset I recommend
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingusingdatasets.asp
http://msdn.microsoft.com/library/en-us/vbcon/html/vboriDatasets.asp

If you only want to process data from the database sequentially, you can use
a DataReader. As the name says, it is used to read the data from the
databse. A dataset is an object that locally stores the content of the
records read, and you'll be able to access the records randomly.

I just found a good book that also has a nice summary of the dataset
object. I hate to ask stupid questions but I'm in that awkward "just
starting" phase where you don't even know what questions to ask let
alone the answers.

Thanks!

Chuck.
 
Back
Top