2011年12月21日 星期三

合計記錄值至另一筆記錄中

Imports System.Data.OleDb

Public Class Form1
    Dim cn As OleDbConnection
    Dim cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\dbtest.mdb;Persist Security Info=True"

    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
    Sub sqlExec(ByVal sqlStr)
        Dim cmd As OleDbCommand = New OleDbCommand(sqlStr, cn)
        cmd.ExecuteNonQuery()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '
        Dim a() = {"明", "天", "明天"}
        Dim f2Value = "後天"
        Call sumTo(a, f2Value)
        Me.資料表1TableAdapter.Fill(Me.DbtestDataSet.資料表1)
    End Sub

    Sub sumTo(ByVal a, ByVal f2Value)
        cn = New OleDbConnection(cnStr)
        cn.Open()
        '
        Dim cmd As New OleDbCommand("SELECT * FROM 資料表1", cn)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        Dim sum = 0
        Do While dr.Read()
            For Each elm In a
                If dr.Item("f2").ToString = elm Then
                    sum = sum + dr.Item(2)
                End If
            Next
        Loop

        Dim sqlStr As String = "update 資料表1 set f3= " & sum & " where f2 =" & "'" & f2Value & "';"
        sqlExec(sqlStr)

        cn.Close()
    End Sub
End Class

image

image

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