Ja, wenn du es alles von Hand machen willst dann bitte keine Beschwerden, dass das umständlich ist! Hier ist aber ein Beispiel. Erstens so geht's mit DataAdapter
Option Strict On
Imports System.ComponentModel
Public Class Form1
Private conn As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=VertragsDB.mdb")
Private da As OleDb.OleDbDataAdapter
Private ds As New DataSet
Private cb As OleDb.OleDbCommandBuilder
Private src As BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
ReadDBWithDataAdapter()
End Sub
Private Sub ReadDBWithDataAdapter()
da = New OleDb.OleDbDataAdapter("SELECT * FROM CValues Order By id", _
conn)
Try
da.Fill(ds, "CValues")
Catch ex As System.Exception
MsgBox("DB read failed: " + ex.ToString())
Return
End Try
src = New BindingSource(ds, "CValues")
DataGridView1.DataSource = src
End Sub
Private Sub DoUpdate_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles DoUpdate.Click
' Update with DataAdapter
If cb Is Nothing Then cb = New OleDb.OleDbCommandBuilder(da)
da.Update(ds, "CValues")
End Sub
End Class Ganze zwei Zeilen um die Daten einzulesen (ohne Try/Catch usw).
Und so würde ich das vielleicht machen mit DataReader. Zuerst eine Klasse für die DB Werte
Private Class CValue
Private _id As Integer
Private _type As String
Private _code As String
Private _value As String
Private _control As Boolean
Private _sort As Integer
Private _changed As Boolean
Public Sub New(ByVal id As Integer, ByVal type As String, ByVal code As _
String, ByVal value As String, ByVal control As Boolean, ByVal sort _
As Integer)
_id = id
_type = type
_code = code
_value = value
_control = control
_sort = sort
End Sub
Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal Value As Integer)
_id = Value
_changed = True
End Set
End Property
Public Property Type() As String
Get
Return _type
End Get
Set(ByVal Value As String)
_type = Value
_changed = True
End Set
End Property
Public Property Code() As String
Get
Return _code
End Get
Set(ByVal Value As String)
_code = Value
_changed = True
End Set
End Property
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal Val As String)
_value = Val
_changed = True
End Set
End Property
Public Property Control() As Boolean
Get
Return _control
End Get
Set(ByVal Value As Boolean)
_control = Value
_changed = True
End Set
End Property
Public Property Sort() As Integer
Get
Return _sort
End Get
Set(ByVal Value As Integer)
_sort = Value
_changed = True
End Set
End Property
<Browsable(False)> _
Public Property Changed() As Boolean
Get
Return _changed
End Get
Set(ByVal value As Boolean)
_changed = value
End Set
End Property
End Class Und so wird's gemacht, auch mit Update und DBNulls
Option Strict On
Imports System.ComponentModel
Public Class Form1
Private conn As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=VertragsDB.mdb")
Private src As BindingSource
Private cvalues As New BindingList(Of CValue)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
IO.File.Copy("F:\Work Dev\VSNet Dev\Test\VertragsDB.mdb", _
Application.StartupPath & "\VertragsDB.mdb", True)
ReadDBWithDataReader()
End Sub
Private Function getVal(Of T)(ByVal rdr As OleDb.OleDbDataReader, ByVal idx _
As Integer) As T
Dim null As T
If rdr.IsDBNull(idx) Then
Return null
Else
Return CType(rdr(idx), T)
End If
End Function
Private Sub ReadDBWithDataReader()
Dim cmd As New OleDb.OleDbCommand("Select * From CValues", conn)
Dim rdr As OleDb.OleDbDataReader
Try
conn.Open()
rdr = cmd.ExecuteReader()
While rdr.Read()
Dim cv As New CValue( _
getVal(Of Integer)(rdr, 0), _
getVal(Of String)(rdr, 1), _
getVal(Of String)(rdr, 2), _
getVal(Of String)(rdr, 3), _
getVal(Of Boolean)(rdr, 4), _
getVal(Of Integer)(rdr, 5))
cvalues.Add(cv)
End While
src = New BindingSource(cvalues, String.Empty)
DataGridView1.DataSource = src
Catch ex As Exception
MsgBox("Read DB failed: " & ex.ToString())
Return
Finally
conn.Close()
End Try
End Sub
Private Sub DoUpdate_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles DoUpdate.Click
' Update with command
Dim cmd As New OleDb.OleDbCommand("UPDATE CValues set type = ?, code =" & _
"?, [value] = ?, control = ?, sort = ? WHERE id = ?", conn)
Dim p1 As OleDb.OleDbParameter = cmd.Parameters.Add("p1", _
OleDb.OleDbType.VarChar)
Dim p2 As OleDb.OleDbParameter = cmd.Parameters.Add("p2", _
OleDb.OleDbType.VarChar)
Dim p3 As OleDb.OleDbParameter = cmd.Parameters.Add("p3", _
OleDb.OleDbType.VarChar)
Dim p4 As OleDb.OleDbParameter = cmd.Parameters.Add("p4", _
OleDb.OleDbType.Boolean)
Dim p5 As OleDb.OleDbParameter = cmd.Parameters.Add("p5", _
OleDb.OleDbType.Integer)
Dim p6 As OleDb.OleDbParameter = cmd.Parameters.Add("p6", _
OleDb.OleDbType.Integer)
Try
conn.Open()
For Each cv As CValue In cvalues
If cv.Changed Then
p1.Value = cv.Type
p2.Value = cv.Code
p3.Value = cv.Value
p4.Value = cv.Control
p5.Value = cv.Sort
p6.Value = cv.Id
Dim cnt As Integer = cmd.ExecuteNonQuery()
If cnt <> 1 Then
MsgBox("Update failed for " & cv.Id.ToString())
Return
End If
cv.Changed = False
End If
Next
Catch ex As Exception
MsgBox("Update DB failed: " & ex.ToString())
Return
Finally
conn.Close()
End Try
End Sub
End Class ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |