Rumah > pembangunan bahagian belakang > Tutorial C#.Net > VB 操作ACCESS 实例 练习题--ATM取款机 代码段

VB 操作ACCESS 实例 练习题--ATM取款机 代码段

黄舟
Lepaskan: 2017-03-02 11:33:14
asal
1634 orang telah melayarinya

====================================
通用
====================================
连接数据库(拼接连接数据库字符串,调用函数即可):

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()
Salin selepas log masuk

验证用户名密码(就是拿着界面上输入的用户名密码 去数据库里面匹配,如果可以找到就通过):

 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()
Salin selepas log masuk
    '验证一下用户名密码是否存在
        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
Salin selepas log masuk

退出(其实就是把所有打开的窗口都隐藏,把登陆窗口显示就可以了):

For Each f As Form In Application.OpenForms
            f.Hide()
            frmLogin.Show()
        Next
Salin selepas log masuk

====================================
客户
====================================
取款:

 '连接数据库
        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()
Salin selepas log masuk

存款:

'连接数据库
        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()
Salin selepas log masuk

查询账户:

'连接数据库
        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()
Salin selepas log masuk

===================================
管理员
==================================
记录操作日志:

 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()
Salin selepas log masuk

查看所有操作日志:

'连接数据库
        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
Salin selepas log masuk

查看所有用户账号

 '连接数据库
        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
Salin selepas log masuk

 以上就是VB  操作ACCESS 实例  练习题--ATM取款机  代码段的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan