VB MYSQL 数据库 增删改查
时间: 2023-08-17 17:22:42 浏览: 106
mysql 增删改查
下面是 VB.NET 与 MySQL 数据库进行增删改查的示例代码:
连接数据库:
```vb
Dim conn As MySqlConnection
Dim connStr As String = "server=localhost;user id=root;password=123456;database=mydb"
conn = New MySqlConnection(connStr)
conn.Open()
```
插入数据:
```vb
Dim sql As String = "INSERT INTO mytable (name, age) VALUES ('张三', 20)"
Dim cmd As New MySqlCommand(sql, conn)
cmd.ExecuteNonQuery()
```
查询数据:
```vb
Dim sql As String = "SELECT * FROM mytable"
Dim cmd As New MySqlCommand(sql, conn)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
While reader.Read()
Dim name As String = reader.GetString("name")
Dim age As Integer = reader.GetInt32("age")
Console.WriteLine("Name: {0}, Age: {1}", name, age)
End While
reader.Close()
```
更新数据:
```vb
Dim sql As String = "UPDATE mytable SET age = 21 WHERE name = '张三'"
Dim cmd As New MySqlCommand(sql, conn)
cmd.ExecuteNonQuery()
```
删除数据:
```vb
Dim sql As String = "DELETE FROM mytable WHERE name = '张三'"
Dim cmd As New MySqlCommand(sql, conn)
cmd.ExecuteNonQuery()
```
关闭数据库连接:
```vb
conn.Close()
```
阅读全文