Here is the code, Button1 will load data from local Access (*.mdb) file, and
Button2 will save modfied data to Access file. But somehow, after I press
Button2, nothing happened. Please help.
Thanks in advance.
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Public ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=D:\test.mdb"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
' & "Data Source=D:\test.mdb"
' Open the Access database.
Dim cn As New OleDbConnection
cn.ConnectionString = ConnString
Try
cn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
' Ensure that the connection is closed.
' (It does not throw an exception even if the Open command failed.)
cn.Close()
End Try
' Define the command
Dim sqlString As String = "SELECT DCN,Field1 from Table1"
Dim da As New OleDbDataAdapter(sqlString, cn)
Dim ds As New DataSet
da.Fill(ds, "Table1")
DataGrid1.DataSource = ds.Tables("Table1")
' Close the connection only if it was opened.
'ConnectionState.Closed = 0
If (cn.State And ConnectionState.Open) <> 0 Then
cn.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim cn As New OleDbConnection(ConnString)
cn.Open()
Dim da As New OleDbDataAdapter("SELECT DCN,Field1 from Table1", cn)
Dim ds As New DataSet
Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
da.Fill(ds, "Table1")
' Update database with modified data
da.ContinueUpdateOnError = True
da.UpdateCommand = cmdBuilder.GetUpdateCommand()
da.Update(ds, "Table1")
' Close the connection only if it was opened.
'ConnectionState.Closed = 0
If (cn.State And ConnectionState.Open) <> 0 Then
cn.Close()
End If
End Sub
On Fri, 25 Jun 2004 17:17:01 -0600,
ja***@hotmail.com wrote:
Hi, all
How can I update data (multiple rows, but not every rows) using dataset in
datagrid?
I mean is there any way I can let datagrid know which row(s)/column(s) has been
modified and update them at database?
Thanks.