'**********************************
'Using OleDb Command
'By:wgscd
'QQ153964481
'*********************************
'先建立一个用ACCACESS建立一个名为“wgscd”的数据库
'字段:编号,电话,地址,QQ,MAIL
'保存路径:C:\me\source_file\wgscd.mdb
'*********************************
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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 Button1 As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents DataGridTableStyle1 As System.Windows.Forms.DataGridTableStyle
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Me.Button1 = New System.Windows.Forms.Button
3Me.DataGrid1 = New System.Windows.Forms.DataGrid
4Me.DataGridTableStyle1 = New System.Windows.Forms.DataGridTableStyle
5Me.Button2 = New System.Windows.Forms.Button
6Me.Button3 = New System.Windows.Forms.Button
7Me.TextBox1 = New System.Windows.Forms.TextBox
8CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
9Me.SuspendLayout()
10'
11'Button1
12'
13Me.Button1.Location = New System.Drawing.Point(528, 24)
14Me.Button1.Name = "Button1"
15Me.Button1.Size = New System.Drawing.Size(88, 32)
16Me.Button1.TabIndex = 1
17Me.Button1.Text = "Delete"
18'
19'DataGrid1
20'
21Me.DataGrid1.BackgroundColor = System.Drawing.Color.Tan
22Me.DataGrid1.CaptionText = "oleDb demo By wgscd"
23Me.DataGrid1.DataMember = ""
24Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
25Me.DataGrid1.Location = New System.Drawing.Point(24, 72)
26Me.DataGrid1.Name = "DataGrid1"
27Me.DataGrid1.Size = New System.Drawing.Size(608, 304)
28Me.DataGrid1.TabIndex = 2
29Me.DataGrid1.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {Me.DataGridTableStyle1})
30'
31'DataGridTableStyle1
32'
33Me.DataGridTableStyle1.BackColor = System.Drawing.Color.Blue
34Me.DataGridTableStyle1.DataGrid = Me.DataGrid1
35Me.DataGridTableStyle1.HeaderBackColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(255, Byte), CType(255, Byte))
36Me.DataGridTableStyle1.HeaderFont = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
37Me.DataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText
38Me.DataGridTableStyle1.LinkColor = System.Drawing.Color.RosyBrown
39Me.DataGridTableStyle1.MappingName = ""
40Me.DataGridTableStyle1.SelectionBackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(255, Byte))
41'
42'Button2
43'
44Me.Button2.Location = New System.Drawing.Point(40, 16)
45Me.Button2.Name = "Button2"
46Me.Button2.Size = New System.Drawing.Size(88, 32)
47Me.Button2.TabIndex = 3
48Me.Button2.Text = "add new row"
49'
50'Button3
51'
52Me.Button3.Location = New System.Drawing.Point(160, 16)
53Me.Button3.Name = "Button3"
54Me.Button3.Size = New System.Drawing.Size(152, 32)
55Me.Button3.TabIndex = 4
56Me.Button3.Text = "insert command "
57'
58'TextBox1
59'
60Me.TextBox1.Location = New System.Drawing.Point(440, 32)
61Me.TextBox1.Name = "TextBox1"
62Me.TextBox1.Size = New System.Drawing.Size(72, 21)
63Me.TextBox1.TabIndex = 5
64Me.TextBox1.Text = "TextBox1"
65'
66'Form1
67'
68Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
69Me.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(224, Byte), CType(192, Byte))
70Me.ClientSize = New System.Drawing.Size(696, 430)
71Me.Controls.Add(Me.TextBox1)
72Me.Controls.Add(Me.Button3)
73Me.Controls.Add(Me.Button2)
74Me.Controls.Add(Me.DataGrid1)
75Me.Controls.Add(Me.Button1)
76Me.Name = "Form1"
77Me.Text = "Form1"
78CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
79Me.ResumeLayout(False)
80
81End Sub
82
83#End Region
84
85Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\me\source_file\wgscd.mdb"
86Dim ds As New DataSet("ds")
87Dim dt As New DataTable("tb")
88Dim oleconn As New OleDbConnection(strconn)
89Dim olecmd As New OleDbCommand("select * from 美女联系方式", oleconn) '女名单") '
90
91Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
92
93
94Try
95
96If oleconn.State = ConnectionState.Open Then
97
98If CInt(TextBox1.Text) > 0 Then
99
100
101olecmd.CommandText = "delete from 美女联系方式 where id=" & TextBox1.Text
102olecmd.ExecuteNonQuery()
103oleconn.Close()
104MsgBox("ID为" & TextBox1.Text & "的记录成功删除!")
105' Me.DataGrid1 = Nothing
106
107' Me.DataGrid1.Refresh()
108
109End If
110
111Else
112
113End If
114
115getDb()
116
117Catch ex As Exception
118MsgBox(ex.Message)
119End Try
120End Sub
121
122Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
123
124getDb()
125
126End Sub
127Dim olead As New OleDbDataAdapter(olecmd)
128
129Sub getDb()
130
131Try
132
133oleconn.Open()
134olead.Fill(ds)
135dt = ds.Tables(0)
136DataGrid1.DataSource = dt
137TextBox1.Text = dt.Rows.Count
138
139Catch ex As Exception
140
141MsgBox(ex.Message)
142
143End Try
144
145End Sub
146
147Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
148
149Dim dr As DataRow = dt.NewRow
150dr(0) = "999"
151dr(1) = "77"
152dr(2) = " 0407"
153dr(3) = "767"
154dr(4) = "737"
155dr(5) = "737"
156dt.Rows.Add(dr)
157dt.AcceptChanges()
158ds.AcceptChanges()
159
160End Sub
161
162Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
163
164
165Try
166
167If oleconn.State = ConnectionState.Open Then
168'添加记录命令串
169olecmd.CommandText = "insert into 美女联系方式 (编号,电话,地址, QQ,MAIL) values (@tid,@phone,@addr,@qq,@mail)"
170olecmd.Parameters.Add("@tid", "66")
171olecmd.Parameters.Add("@phone", "665454").DbType = DbType.String
172olecmd.Parameters.Add("@addr", "swust")
173olecmd.Parameters.Add("@qq", "355566")
174olecmd.Parameters.Add("@mail", " [email protected] ")
175olecmd.ExecuteNonQuery()
176oleconn.Close()
177
178End If
179
180Catch ex As Exception
181MsgBox(ex.Message)
182
183
184End Try
185
186End Sub
187End Class</system.diagnostics.debuggerstepthrough()>