VB 操作ACCESS 執行個體 練習題--ATM取款機 程式碼片段

來源:互聯網
上載者:User
====================================
通用
====================================
串連資料庫(拼接串連資料庫字串,調用函數即可):

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取款機 程式碼片段的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.