VB 实例学习笔记

简介

Visual Basic(VB)是一种非常流行的事件驱动编程语言,它可以用来开发桌面应用程序、Web 应用程序和数据库应用程序等。在本学习笔记中,我们将通过多个实例来学习 VB 编程。

实例 1:计算器应用程序

这个实例展示了如何使用 VB 来创建一个简单的计算器应用程序。在这个应用程序中,用户将能够输入两个数字,选择一个运算符(如加、减、乘、除),然后计算结果。

Copy Code
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' 获取用户输入的两个数字 Dim num1 As Double = Double.Parse(TextBox1.Text) Dim num2 As Double = Double.Parse(TextBox2.Text) ' 根据用户选择的操作符来计算结果 Dim result As Double If RadioButton1.Checked Then result = num1 + num2 ElseIf RadioButton2.Checked Then result = num1 - num2 ElseIf RadioButton3.Checked Then result = num1 * num2 ElseIf RadioButton4.Checked Then result = num1 / num2 End If ' 将结果显示在 Label 控件上 Label1.Text = "计算结果:" & result.ToString() End Sub End Class

实例 2:图书管理系统

这个实例展示了如何使用 VB 和 Microsoft Access 数据库来创建一个简单的图书管理系统。在这个应用程序中,用户可以添加、修改和删除图书记录,并查询图书库存。

Copy Code
Public Class Form1 Dim conn As New OleDb.OleDbConnection Dim da As OleDb.OleDbDataAdapter Dim ds As New DataSet Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' 连接到数据库 conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BookDatabase.accdb" conn.Open() ' 加载数据到 DataGridView 控件上 Dim sql As String = "SELECT * FROM Books" da = New OleDb.OleDbDataAdapter(sql, conn) da.Fill(ds, "Books") DataGridView1.DataSource = ds.Tables("Books") ' 关闭数据库连接 conn.Close() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' 连接到数据库 conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BookDatabase.accdb" conn.Open() ' 插入新的图书记录 Dim cmd As New OleDb.OleDbCommand cmd.Connection = conn cmd.CommandText = "INSERT INTO Books (Title, Author, Publisher, Price) VALUES (@Title, @Author, @Publisher, @Price)" cmd.Parameters.AddWithValue("@Title", TextBox1.Text) cmd.Parameters.AddWithValue("@Author", TextBox2.Text) cmd.Parameters.AddWithValue("@Publisher", TextBox3.Text) cmd.Parameters.AddWithValue("@Price", TextBox4.Text) cmd.ExecuteNonQuery() ' 加载数据到 DataGridView 控件上 Dim sql As String = "SELECT * FROM Books" da = New OleDb.OleDbDataAdapter(sql, conn) ds.Tables("Books").Clear() da.Fill(ds, "Books") DataGridView1.DataSource = ds.Tables("Books") ' 关闭数据库连接 conn.Close() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ' 连接到数据库 conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BookDatabase.accdb" conn.Open() ' 更新选中的图书记录 Dim cmd As New OleDb.OleDbCommand cmd.Connection = conn cmd.CommandText = "UPDATE Books SET Title=@Title, Author=@Author, Publisher=@Publisher, Price=@Price WHERE ID=@ID" cmd.Parameters.AddWithValue("@Title", TextBox1.Text) cmd.Parameters.AddWithValue("@Author", TextBox2.Text) cmd.Parameters.AddWithValue("@Publisher", TextBox3.Text) cmd.Parameters.AddWithValue("@Price", TextBox4.Text) cmd.Parameters.AddWithValue("@ID", DataGridView1.SelectedCells(0).Value) cmd.ExecuteNonQuery() ' 加载数据到 DataGridView 控件上 Dim sql As String = "SELECT * FROM Books" da = New OleDb.OleDbDataAdapter(sql, conn) ds.Tables("Books").Clear() da.Fill(ds, "Books") DataGridView1.DataSource = ds.Tables("Books") ' 关闭数据库连接 conn.Close() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click ' 连接到数据库 conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BookDatabase.accdb" conn.Open() ' 删除选中的图书记录 Dim cmd As New OleDb.OleDbCommand cmd.Connection = conn cmd.CommandText = "DELETE FROM Books WHERE ID=@ID" cmd.Parameters.AddWithValue("@ID", DataGridView1.SelectedCells(0).Value) cmd.ExecuteNonQuery() ' 加载数据到 DataGridView 控件上 Dim sql As String = "SELECT * FROM Books" da = New OleDb.OleDbDataAdapter(sql, conn) ds.Tables("Books").Clear() da.Fill(ds, "Books") DataGridView1.DataSource = ds.Tables("Books") ' 关闭数据库连接 conn.Close() End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click ' 连接到数据库 conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BookDatabase.accdb" conn.Open() ' 根据关键字查询图书记录 Dim sql As String = "SELECT * FROM Books WHERE Title LIKE '%" & TextBox5.Text & "%'" da = New OleDb.OleDbDataAdapter(sql, conn) ds.Tables("Books").Clear() da.Fill(ds, "Books") DataGridView1.DataSource = ds.Tables("Books") ' 关闭数据库连接 conn.Close() End Sub End Class

结论

通过以上两个示例,我们可以看到 VB 是一种非常有效的编程语言,可以用于创建各种类型的应用程序。希望这些学习笔记能够帮助你开始学习 VB 编程。