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

📄 form1.vb

📁 黑魔方vb.net数据库开发第5章第1节
💻 VB
字号:
Imports System.Data.OleDb
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim fullname = Application.StartupPath()
    Dim myfiledir As New System.IO.DirectoryInfo(fullname)
    Dim dbfile = myfiledir.Parent.Parent.FullName & "\northwind.mdb"
    Dim constr As String = "provider=microsoft.jet.oledb.4.0;data source=" & dbfile
    '定义连接
    Dim objconn As New OleDbConnection(constr)
    '定义数据适配器
    Dim objadapter As New OleDbDataAdapter("SELECT * From 产品", objconn)
    Dim cmd As New OleDbCommand()
    '定义数据集
    Dim ds1 As New DataSet()
    Sub showdata()
        Dim sqlstr = "select * from 产品 where 产品名称='" & ComboBox1.Text & "'"
        cmd = New OleDbCommand(sqlstr, objconn)
        objadapter.SelectCommand = cmd
        objadapter.Fill(ds1)
        DataGrid1.DataSource = ds1.Tables(0)
    End Sub
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents DataGrid2 As System.Windows.Forms.DataGrid
    Friend WithEvents Button4 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        Me.DataGrid2 = New System.Windows.Forms.DataGrid()
        Me.Button4 = New System.Windows.Forms.Button()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'ComboBox1
        '
        Me.ComboBox1.Location = New System.Drawing.Point(408, 24)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(208, 23)
        Me.ComboBox1.TabIndex = 1
        Me.ComboBox1.Text = "ComboBox1"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(240, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(152, 32)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "请选择产品名"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(152, 272)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(136, 32)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "显示数据"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 64)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(440, 192)
        Me.DataGrid1.TabIndex = 5
        '
        'DataGrid2
        '
        Me.DataGrid2.DataMember = ""
        Me.DataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid2.Location = New System.Drawing.Point(472, 64)
        Me.DataGrid2.Name = "DataGrid2"
        Me.DataGrid2.Size = New System.Drawing.Size(336, 192)
        Me.DataGrid2.TabIndex = 6
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(600, 272)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(88, 32)
        Me.Button4.TabIndex = 7
        Me.Button4.Text = "建立新表"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 18)
        Me.ClientSize = New System.Drawing.Size(816, 320)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button4, Me.DataGrid2, Me.DataGrid1, Me.Button3, Me.Label1, Me.ComboBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        objconn.Open()
        Dim objCmd As New OleDbCommand("SELECT * From 产品", objconn)
        Dim objreader As OleDbDataReader
        objreader = objCmd.ExecuteReader
        While objreader.Read
            ComboBox1.Items.Add(objreader.GetString(1))
        End While
        objreader.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        showdata()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '建立一个新表people,将表增加到Dataset中
        Dim dtable As New DataTable("雇员")
        With dtable.Columns
            .Add("雇员编号", System.Type.GetType("System.Int32"))
            .Add("姓名", System.Type.GetType("System.String"))
            .Add("性别", System.Type.GetType("System.String"))
            .Add("出生年月", System.Type.GetType("System.DateTime"))
        End With
        dtable.Columns("雇员编号").AutoIncrement = True
        '判断dtable是否已归ds1
        If Not ds1.Tables.Contains("雇员") Then
            ds1.Tables.Add(dtable)
        End If

        Dim pkey() As DataColumn = {ds1.Tables("雇员").Columns("雇员编号")}
        ds1.Tables("雇员").PrimaryKey = pkey
        '往新建的雇员表中增加一条记录
        Dim newrow As DataRow = dtable.NewRow
        newrow(1) = "张三"
        newrow(2) = "男"
        newrow(3) = "1980-7-8"
        dtable.Rows.Add(newrow)
        DataGrid2.DataSource = ds1.Tables("雇员")
    End Sub

 End Class

⌨️ 快捷键说明

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