VB 操作ACCESS 實例 練習題--ATM提款機 代碼段

黄舟
發布: 2017-03-02 11:33:14
原創
1612 人瀏覽過

====================================
通用
==== ================================
連接資料庫(拼接連接資料庫字串,呼叫函數即可):

Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()
登入後複製

驗證使用者名稱密碼(就是拿著介面上輸入的使用者名稱密碼 去資料庫裡面匹配,如果可以找到就通過):

 Private Function UserIsValid() As Boolean
        '连接数据库
        Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()
登入後複製
    '验证一下用户名密码是否存在
        Dim strSql As String = "select * from AccountInfo where AccountNo = '" & _
       Trim(TextBox1.Text) & "' and Password = '" & Trim(TextBox2.Text) & "'"
        Dim connCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSql, connConnection)
        Dim myReader As OleDb.OleDbDataReader = connCommand.ExecuteReader


        '如果有数据
        If myReader.HasRows Then
            myReader.Read() '读一行数据到reader里面
            If Trim(TextBox1.Text) = myReader(1) And Trim(TextBox2.Text) = myReader(2) Then
                connConnection.Close()
                Return True
            Else
                connConnection.Close()
                Return False
            End If


        End If
        connConnection.Close()
        '没数据直接返回错误
        Return False

    End Function
登入後複製

退出(其實就是把所有打開的視窗隱藏,把登陸視窗顯示就可以了):

For Each f As Form In Application.OpenForms
            f.Hide()
            frmLogin.Show()
        Next
登入後複製

======================== ==============
客戶
============================= =======
提款:

 '连接数据库
        Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()


        '修改金额(存钱)
        Dim sql As String = "update AccountInfo set Balance = Balance + " + TextBox1.Text + " where AccountNo = '" + frmLogin.TextBox1.Text + "'"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, connConnection)
        cmd.ExecuteNonQuery()


        '记录操作日志
        sql = "insert into AccountAct (AccountNo,Lastopt,Amount) values ('" & Trim(frmLogin.TextBox1.Text) & "','" & Trim(DateTime.Now.ToString()) & "',
        " & CType(TextBox1.Text, Double) & ")"
        Dim cmd2 As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, connConnection)
        cmd2.ExecuteNonQuery()


        connConnection.Close()
登入後複製

存款:

'连接数据库
        Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()


        '修改金额(取钱)
        Dim sql As String = "update AccountInfo set Balance = Balance - " + TextBox2.Text + " where AccountNo = '" + frmLogin.TextBox1.Text + "'"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, connConnection)
        cmd.ExecuteNonQuery()
        '记录操作日志
        sql = "insert into AccountAct (AccountNo,Lastopt,Amount) values ('" & Trim(frmLogin.TextBox1.Text) & "','" & Trim(DateTime.Now.ToString()) & "',
        " & CType(TextBox1.Text, Integer) & ")"
        cmd = New OleDb.OleDbCommand(sql, connConnection)
        cmd.ExecuteNonQuery()


        connConnection.Close()
登入後複製

查詢帳戶:
##

'连接数据库
        Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()


        Dim sql As String = "select Balance from AccountInfo where AccountNo = '" + frmLogin.TextBox1.Text + "'"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, connConnection)
        Dim myReader As OleDb.OleDbDataReader = cmd.ExecuteReader
        If myReader.HasRows Then
            myReader.Read()
            Label1.Text = myReader(0)
        Else
            MessageBox.Show("未找到账户信息")
        End If


        connConnection.Close()
登入後複製

=== ================================

管理員
========== ========================
記錄操作日誌:

 sql = "insert into AccountAct (AccountNo,Lastopt,Amount) values ('" & Trim(frmLogin.TextBox1.Text) & "','" & Trim(DateTime.Now.ToString()) & "',
 " & CType(TextBox1.Text, Integer) & ")"
        cmd = New OleDb.OleDbCommand(sql, connConnection)
        cmd.ExecuteNonQuery()


        connConnection.Close()
登入後複製

#查看所有操作日誌:


'连接数据库
        Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()


        '把查询的数据取出来放到data set
        Dim strSql As String = "select * from AccountAct"
        Dim myDataSet As DataSet = New DataSet()
        Dim myAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSql, connConnection)
        myAdapter.Fill(myDataSet, "AccountAct")
        connConnection.Close()


        '把数据从dataset中取出来显示在控件上()
        DataGridView1.DataSource = myDataSet.Tables(0)
        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
登入後複製

查看所有使用者帳號


 '连接数据库
        Dim Con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ATM.mdb"
        Dim connConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        connConnection.ConnectionString = Con
        connConnection.Open()


        '把查询的数据取出来放到data set
        Dim strSql As String = "select * from AccountInfo"
        Dim myDataSet As DataSet = New DataSet()
        Dim myAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSql, connConnection)
        myAdapter.Fill(myDataSet, "AccountInfo")
        connConnection.Close()


        '把数据从dataset中取出来显示在控件上()
        DataGridView1.DataSource = myDataSet.Tables(0)
        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
登入後複製
 以上就是VB  操作ACCESS 實例 練習題--ATM提款機 代碼段的內容,更多相關內容請關注PHP中文網(www. php.cn)!



#

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!