Updating Databases in VB.NET

T

TDS News

Here's my code...

Private oConn As New OleDb.OleDbConnection()
Private oCommand As New OleDb.OleDbCommand()
Private oDataAdapter As New OleDb.OleDbDataAdapter()
Private oDataSet As New DataSet()

Public Event Err()

Public Sub Init()

Dim Row As DataRow

oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Application.StartupPath & "\WinMUX.mdb;Persist Security
Info=False"

oCommand.CommandText = "SELECT Name FROM Objects"
oCommand.Connection = oConn
oCommand.CommandTimeout = 30
oDataAdapter.SelectCommand = oCommand
oDataAdapter.Fill(oDataSet, "Objects")

For Each Row In oDataSet.Tables("Objects").Rows
MsgBox(Row("Name"))
If Row("Name") = "Brimstone" Then
Row("Name") = "NotBrimstone"
End If
Next
oDataAdapter.Update(oDataSet, "Objects")

End Sub

I know it isn't the prettiest code but I'm just trying to figure this Data
Access stuff out in .NET. How do I update the data in the database with
what has been changed in the Dataset? I'm completely lost. With ADO you
used to just call the adoRecordset.Update method. What do you do now?


Andrew Cooper
(e-mail address removed)
 
A

Armin Zingler

TDS News said:
oDataAdapter.Update(oDataSet, "Objects")

I know it isn't the prettiest code but I'm just trying to figure this
Data Access stuff out in .NET. How do I update the data in the
database with what has been changed in the Dataset? I'm completely
lost. With ADO you used to just call the adoRecordset.Update method.
What do you do now?


The code looks good. I didn't test it but it should work - or does it not?
oDataAdapter.Update should update the data source.
 
C

Cor

Hi,

I was first thinking the same as Armin and fortunatly I saw it, there was no
commandbuilder. (That makes all kind of commands from your select)
I also added something about haschanges and getchanges, but that is not the
reason why it is not updating, that is just ment as a little bit optimizing.

Watch typos, because I did type it in your style.

I hope this helps,

Cor
Private oConn As New OleDb.OleDbConnection()
Private oCommand As New OleDb.OleDbCommand()
Private oDataAdapter As New OleDb.OleDbDataAdapter()
\\
Private oCommandBuilder As OleDbCommandBuilder
//
Private oDataSet As New DataSet()

Public Event Err()

Public Sub Init()

Dim Row As DataRow

oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Application.StartupPath & "\WinMUX.mdb;Persist Security
Info=False"

oCommand.CommandText = "SELECT Name FROM Objects"
oCommand.Connection = oConn
oCommand.CommandTimeout = 30
oDataAdapter.SelectCommand = oCommand
\\
oCommandBuilder = New OleDbCommandBuilder(oDataAdapter)
//
If oDataset.HasChanges Then
da.Update(oDatasetds.GetChanges)
End If
oDataAdapter.Fill(oDataSet.GetChanges, "Objects")
End if
For Each Row In oDataSet.Tables("Objects").Rows
MsgBox(Row("Name"))
If Row("Name") = "Brimstone" Then
Row("Name") = "NotBrimstone"
End If
Next
\\
If oDataset.HasChanges Then
oDataAdapter.Update(oDataset.GetChanges,"Objects")
End If
//
 
A

Armin Zingler

Cor said:
I was first thinking the same as Armin and fortunatly I saw it, there
was no commandbuilder. (That makes all kind of commands from your
select)


Thx. :)
 
A

Andrew Cooper

Cor,

Thanks! That helped a whole lot. I'm finally getting the hang of it.
The CommandBuilder was a huge time saver. I was building every command by
hand and that was very tedious.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top