Maison > Article > développement back-end > Exemples d'exercices d'accès à l'opération VB - Extrait de code de distributeur automatique de billets ATM
=====================================
Universel
==== ================================
Connectez-vous à la base de données (épissez simplement la chaîne de connexion à la base de données et appelez la fonction ):
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()
Vérifiez le nom d'utilisateur et le mot de passe (c'est-à-dire prenez le nom d'utilisateur et le mot de passe saisis sur l'interface et faites-les correspondre dans la base de données. S'ils peuvent être trouvés, transmettez):
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
Quitter (en fait, masquer simplement toutes les fenêtres ouvertes et afficher la fenêtre de connexion) :
For Each f As Form In Application.OpenForms f.Hide() frmLogin.Show() Next
========= ===== =======================
Client
=============== ===== ================
Retrait :
'连接数据库 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()
Dépôt :
'连接数据库 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()
Demande de compte :
'连接数据库 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()
================================= =====
Administrateur
===================================
Enregistrement journal des opérations :
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()
Afficher tous les journaux d'opérations :
'连接数据库 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
Afficher tous les comptes d'utilisateurs
'连接数据库 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
Ce qui précède sont des exemples d'accès aux opérations VB, des questions pratiques - -Le contenu du segment de code du distributeur automatique de billets Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois (www.php.cn) !