?? form1.vb
字號(hào):
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)
'定義數(shù)據(jù)適配器
Dim objadapter As New OleDbDataAdapter("SELECT * From 產(chǎn)品", objconn)
Dim cmd As New OleDbCommand()
'定義數(shù)據(jù)集
Dim ds1 As New DataSet()
Sub showdata()
Dim sqlstr = "select * from 產(chǎn)品 where 產(chǎn)品名稱='" & ComboBox1.Text & "'"
cmd = New OleDbCommand(sqlstr, objconn)
objadapter.SelectCommand = cmd
objadapter.Fill(ds1)
DataGrid1.DataSource = ds1.Tables(0)
End Sub
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent()
'在 InitializeComponent() 調(diào)用之后添加任何初始化
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 窗體設(shè)計(jì)器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下過程是 Windows 窗體設(shè)計(jì)器所必需的
'可以使用 Windows 窗體設(shè)計(jì)器修改此過程。
'不要使用代碼編輯器修改它。
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 = "請(qǐng)選擇產(chǎn)品名"
'
'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 = "顯示數(shù)據(jù)"
'
'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 產(chǎn)品", 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
'建立一個(gè)新表people,將表增加到Dataset中
Dim dtable As New DataTable("雇員")
With dtable.Columns
.Add("雇員編號(hào)", 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("雇員編號(hào)").AutoIncrement = True
'判斷dtable是否已歸ds1
If Not ds1.Tables.Contains("雇員") Then
ds1.Tables.Add(dtable)
End If
Dim pkey() As DataColumn = {ds1.Tables("雇員").Columns("雇員編號(hào)")}
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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -