⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.vb

📁 VB Accese常用操作VB Accese常用操作VB Accese常用操作VB Accese常用操作
💻 VB
字号:
Imports System.Data.OleDb

Public Class Form2

    Dim strCon As String = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =db1.mdb"

    



    Dim strCom1 As String = " SELECT id, xm as 姓名, xb as 性别 ,nl as 年龄 ,zip as 邮政编号  FROM person  "



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Function estimate_Form() As Boolean
        If TextBox1.Text.Trim() <> "" And ComboBox1.Text <> "" And TextBox2.Text.Trim() <> "" And TextBox3.Text.Trim() <> "" Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub From1_gridview()
        '刷新Form1 的数据显示
        Dim Frm1 As New Form1()
        Frm1.bind_data()
    End Sub
    Private Sub Add_Record()
        '新增学生记录
        If estimate_Form() Then
            Dim myConn As OleDbConnection = New OleDbConnection()
            myConn.ConnectionString = strCon
            myConn.Open()
            Dim strInsert As String = " INSERT INTO person  ( id  , xm  ,    xb  , nl  , zip ) VALUES  ( "
            '编号
            strInsert = strInsert & " '" + System.Guid.NewGuid().ToString() & "' ,"
            '姓名
            strInsert = strInsert & " '" + TextBox1.Text.Trim() & "' ,"
            '性别
            strInsert = strInsert & " '" + ComboBox1.Text & "' ,"
            '年龄
            strInsert = strInsert & " '" + TextBox2.Text.Trim() & "' ,"
            '邮政编码
            strInsert = strInsert & " '" + TextBox2.Text.Trim() & "' "
            strInsert = strInsert & ")"
            MessageBox.Show(strInsert)
            Dim inst As OleDbCommand = New OleDbCommand(strInsert, myConn)
            inst.ExecuteNonQuery()
            myConn.Close()
            '新增完成 界面成为修改状态
            TextBox1.Tag = "Modify"
            From1_gridview()
        Else
            MessageBox.Show("必须填满所有字段值!", "错误!")
        End If
    End Sub
    Private Sub Modify_Record()
        '修改学生记录
        If estimate_Form() Then
            Dim myConn As OleDbConnection = New OleDbConnection()
            myConn.ConnectionString = strCon
            myConn.Open()
            Dim strUpdt As String = " UPDATE person SET xm ="
            '姓名
            strUpdt = strUpdt & " '" + TextBox1.Text.Trim() & "' , xb="
            '性别
            strUpdt = strUpdt & " '" + ComboBox1.Text & "' , nl="
            '年龄
            strUpdt = strUpdt & " '" + TextBox2.Text & "' , zip="
            '邮政编码
            strUpdt = strUpdt & " '" + TextBox3.Text & "' "

            strUpdt = strUpdt & " where id='" + TextBox2.Tag.ToString() + "'"
            MessageBox.Show(strUpdt)
            If MessageBox.Show("确定要更新?", "提示", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim myCommand As OleDbCommand = New OleDbCommand(strUpdt, myConn)
                myCommand.ExecuteNonQuery()
            End If
            myConn.Close()


        Else
            MessageBox.Show("必须填满所有字段值!", "错误!")
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '保存学生
        If TextBox1.Tag.ToString() = "New" Then
            Add_Record()
        Else
            'MessageBox.Show(TextBox2.Tag.ToString())
            Modify_Record()
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Init_From()
    End Sub
    Private Sub Init_Update()
        '修改学生信息
        If (GroupBox1.Text <> "修改学生信息") Then
            Dim myDataSet As DataSet
            Dim myConn As OleDbConnection = New OleDbConnection()
            myConn.ConnectionString = strCon
            strCom1 = strCom1 & " where id='" + TextBox2.Tag.ToString() + "'"
            '创建一个 DataSet
            myDataSet = New DataSet()
            myConn.Open()
            '通过OleDbDataAdapter对象得到一个数据集
            Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter(strCom1, myConn)
            '把Dataset绑定books数据表
            myCommand.Fill(myDataSet, "person")
            TextBox1.Text = myDataSet.Tables(0).Rows(0)("姓名").ToString()
            ComboBox1.Text = myDataSet.Tables(0).Rows(0)("性别").ToString()
            TextBox2.Text = myDataSet.Tables(0).Rows(0)("年龄").ToString()
            TextBox3.Text = myDataSet.Tables(0).Rows(0)("邮政编号").ToString()
            myConn.Close()
        End If
      

    End Sub

    Private Sub Init_From()
        If TextBox1.Tag.ToString() = "New" Then
            GroupBox1.Text = "新增学生信息"
        Else
            Init_Update()
            GroupBox1.Text = "修改学生信息"
        End If
    End Sub
    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
        Init_From()
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -