这个简单程序有两块,一块是插入到数据库(Access)中,二块是显示数据库的内容。
- Imports System.Data.OleDb
- Public Class Form1
- Dim cn As OleDbConnection
- Dim cm As OleDbCommand
- Dim da As OleDbDataAdapter
- Dim ds As DataSet
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- TextBox1.Text = ""
- TextBox2.Text = ""
- TextBox3.Text = ""
- TextBox4.Text = ""
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\lyb.mdb;Persist Security Info=False")
- da = New OleDbDataAdapter("select * from lyb order by 时间 desc", cn)
- ds = New DataSet()
- da.Fill(ds, "lyb")
- DataGridView1.DataSource = ds.Tables("lyb")
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim Name As String = TextBox1.Text
- Dim Email As String = TextBox2.Text
- Dim Subject As String = TextBox3.Text
- Dim Memo As String = TextBox4.Text
- If Name = "" Or Email = "" Or Subject = "" Or Memo = "" Then
- MessageBox.Show("字段有未填写,请再次填写!")
- Exit Sub
- End If
- 'Try
- cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\lyb.mdb;Persist Security Info=False")
- cn.Open() '插入前,必须连接
- Dim sql As String = "insert into lyb (姓名,Email,主题,内容) values('" & Name & "','" & Email & "','" & Subject & "','" & Memo & "')"
- cm = New OleDbCommand(sql, cn)
- cm.ExecuteNonQuery()
- cn.Close()
- MessageBox.Show("保存成功!!!")
- 'Catch ex As Exception
- ' MessageBox.Show("保存失败!")
- 'End Try
- End Sub
- End Class
运营不易,
感谢支持!
我的微信
我的微信公众号
我的微信公众号扫一扫
我的公众号