2011年12月1日 星期四

SQL指令Update Access 資料庫

Imports System.Data.OleDb

Public Class Form1
    Private Sub 資料表1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 資料表1BindingNavigatorSaveItem.Click
        Me.Validate()
        Me.資料表1BindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbtestDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: 這行程式碼會將資料載入 'DbtestDataSet.資料表1' 資料表。您可以視需要進行移動或移除。
        Me.資料表1TableAdapter.Fill(Me.DbtestDataSet.資料表1)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cn As OleDbConnection
        Dim cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\dbtest.mdb;Persist Security Info=True"
        cn = New OleDbConnection(cnStr)
        cn.Open()
        Dim f2Value = "fff"
        Dim f3Value = 20
        Dim sqlStr As String = "update 資料表1 set f2= '" & f2Value & "' where f3 >" & f3Value
        Dim cmd As OleDbCommand = New OleDbCommand(sqlStr, cn)
        cmd.ExecuteNonQuery()
        cn.Close()

        Me.資料表1TableAdapter.Fill(Me.DbtestDataSet.資料表1)
    End Sub
End Class

image

沒有留言: