数据库开发003简单的留言系统

数据库开发003简单的留言系统_第1张图片

Imports System.Data.OleDb
Public Class Form1

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim Name As String = txtName.Text
        Dim Email As String = txtEmail.Text
        Dim Subject As String = txtTitle.Text
        Dim Memo As String = txtMemo.Text
        If Name = "" Or Email = "" Or Subject = "" Or Memo = "" Then
            MsgBox("字段不能为空")
            Exit Sub
        End If
        Try
            conn = GetConnection()
            conn.Open()
            Dim strcomm As String = "Insert Into lyb (name,Email,Subject,content) Values(@name,@Email,@Subject,@Memo)"
            comm = New OleDbCommand(strcomm, conn)
            Dim p1 As OleDbParameter = New OleDbParameter("@name", Name)
            Dim p2 As OleDbParameter = New OleDbParameter("@Email", Email)
            Dim p3 As OleDbParameter = New OleDbParameter("@Subject", Subject)
            Dim p4 As OleDbParameter = New OleDbParameter("@content", Memo)
            comm.Parameters.Add(p1)
            comm.Parameters.Add(p2)
            comm.Parameters.Add(p3)
            comm.Parameters.Add(p4)

            comm.ExecuteNonQuery()

            conn.Close()
            MsgBox("保存成功")
        Catch ex As Exception
            MsgBox("保存失败")
        End Try
    End Sub
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Form2.Show()
    End Sub


    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtName.Text = ""
        txtEmail.Text = ""
        txtTitle.Text = ""
        txtMemo.Text = ""
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtName.Text = "李三"
        txtEmail.Text = "[email protected] "
        txtTitle.Text = "Hello"
        txtMemo.Text = "How are you."
    End Sub
End Class

数据库开发003简单的留言系统_第2张图片

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ds.Tables.Clear()
        Dim strcomm As String = "Select * from lyb"
        dataAdapter = CreateDataAdpapter(strcomm, GetConnection)
        dataAdapter.Fill(ds, "lyb")

        DataGridView1.DataSource = ds.Tables("lyb")
    End Sub
End Class

模块代码

Imports System.Data.OleDb
Module Module1
    Public conn As OleDbConnection
    Public comm As OleDbCommand
    Public dataAdapter As OleDbDataAdapter
    Public ds As New DataSet
    Public Function GetConnection() As OleDbConnection
        Return New OleDbConnection(My.Settings.lybConnectionString)
    End Function
    Public Function CreateDataAdpapter(ByVal strcomm As String, ByVal conn As OleDbConnection) As OleDbDataAdapter
        Return New OleDbDataAdapter(strcomm, conn)
    End Function
End Module

 

你可能感兴趣的:(#,VB2010数据库开发入门)