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

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 = "a@163.com "
        txtTitle.Text = "Hello"
        txtMemo.Text = "How are you."
    End Sub
End Class

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ngbshzhn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值