代码:
1
Public
Class
Form1
2
3
Dim
mydb
As
dao.Database
'
数据库对象
4
Dim
myws
As
dao.Workspace
'
工作空间,也就是 会话
5
Dim
mysql
As
String
'
保存sql查询的变量
6
Dim
myrs
As
dao.Recordset
'
记录集对象
7
Dim
myengine
As
dao.DBEngine
=
New
dao.DBEngine()
'
引擎,直接初始化
8
9
Public
Function
OpenDB()
As
Short
'
用于连接数据库
10
'
Dim DBPath As String = "G:\liz\DBs\学生名册.mdb"
11
Dim
DBPath
As
String
=
Application.StartupPath
&
"
\学生名册.mdb
"
12
myws
=
myengine.Workspaces(
0
)
'
取当前会话
13
mydb
=
myws.OpenDatabase(DBPath,
False
,
False
)
14
mysql
=
"
select * from 学生名册
"
15
myrs
=
mydb.OpenRecordset(mysql, dao.RecordsetTypeEnum.dbOpenDynaset)
'
用SQL,并使用表类型记录集
16
'
displayStats(myrs.RecordCount)
17
End Function
18
Public
Sub
FillControl()
'
填充显示界面
19
TextBox1.Text
=
myrs.Fields(
0
).Value.ToString
20
TextBox2.Text
=
myrs.Fields(
1
).Value.ToString
21
TextBox3.Text
=
myrs.Fields(
3
).Value.ToString.Substring(
0
,
9
)
22
TextBox4.Text
=
myrs.Fields(
2
).Value.ToString
23
End Sub
24
Public
Sub
New
()
25
'
此调用是 Windows 窗体设计器所必需的。
26
InitializeComponent()
27
'
在 InitializeComponent() 调用之后添加任何初始化。
28
Dim
myresult
As
Short
'
在New里打开数据库
29
myresult
=
OpenDB()
30
End Sub
31
32
Private
Sub
Form1_Activated(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Activated
33
FillControl()
'
激活窗体时填充显示控件
34
End Sub
35
36
Private
Sub
Button1_Click(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
37
'
向前浏览记录
38
If
myrs.BOF
Then
39
Beep
()
40
myrs.MoveFirst()
'
这个是因为,BOF在第一条记录的前面,不用这个,读记录集会出错
41
Else
42
myrs.MovePrevious()
43
If
myrs.BOF
Then
44
myrs.MoveFirst()
45
Exit Sub
46
End
If
47
FillControl()
48
End
If
49
End Sub
50
51
Private
Sub
Button2_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button2.Click
52
'
先后浏览
53
If
myrs.EOF
Then
54
Beep
()
55
myrs.MoveLast()
56
Else
57
myrs.MoveNext()
58
If
myrs.EOF
=
True
Then
59
myrs.MoveLast()
60
Exit Sub
61
End
If
62
FillControl()
63
End
If
64
End Sub
65
66
Private
Sub
Button3_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button3.Click
67
myrs.MoveFirst()
68
FillControl()
69
End Sub
70
71
Private
Sub
Button4_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button4.Click
72
myrs.MoveLast()
73
FillControl()
74
End Sub
75
76
Private
Sub
Button5_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button5.Click
77
Dim
sql1
As
String
=
"
select * from 学生名册 where 学号='
"
&
TextBox1.Text
&
"
'
"
78
Dim
rs1
As
dao.Recordset
79
rs1
=
mydb.OpenRecordset(sql1, dao.RecordsetTypeEnum.dbOpenDynaset)
80
If
rs1.EOF
Then
81
myrs.AddNew()
82
myrs.Fields(
0
).Value
=
TextBox1.Text
83
myrs.Fields(
1
).Value
=
TextBox2.Text
84
myrs.Fields(
3
).Value
=
TextBox3.Text
85
myrs.Fields(
2
).Value
=
TextBox4.Text
86
myrs.Update()
87
Else
88
MsgBox
(
"
这个学号已经存在
"
)
89
Exit Sub
90
End
If
91
ClearTextBoxs()
92
TextBox1.Focus()
93
End Sub
94
Public
Sub
ClearTextBoxs()
95
Dim
i
As
Integer
96
Dim
count
As
Integer
=
Me
.Controls.Count
97
Dim
tb
As
TextBox
98
For
i
=
0
To
count
-
1
99
If
TypeOf
Me
.Controls(i)
Is
TextBox
Then
100
tb
=
CType
(
Me
.Controls(i), TextBox)
101
tb.Text
=
""
102
End
If
103
Next
104
End Sub
105
106
Private
Sub
Button9_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button9.Click
107
End
108
'
另外一种写法是:Application.Exit
109
End Sub
110
111
Private
Sub
Button6_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button6.Click
112
Dim
sql1
As
String
=
"
select * from 学生名册 where 学号='
"
&
TextBox1.Text
&
"
'
"
113
Dim
rs1
As
dao.Recordset
=
mydb.OpenRecordset(sql1, dao.RecordsetTypeEnum.dbOpenDynaset)
114
If
rs1.EOF
Then
115
MsgBox
(
"
查无此人
"
, MsgBoxStyle.OkOnly)
116
Exit Sub
117
Else
118
rs1.Delete()
119
reConnect()
120
FillControl()
121
End
If
122
End Sub
123
Public
Sub
reConnect()
124
mysql
=
"
select * from 学生名册
"
125
myrs
=
mydb.OpenRecordset(mysql, dao.RecordsetTypeEnum.dbOpenDynaset)
126
myrs.MoveFirst()
127
End Sub
128
129
Private
Sub
Button7_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button7.Click
130
Dim
sql
As
String
=
"
select * from 学生名册 where 学号='
"
&
TextBox1.Text
&
"
'
"
131
Dim
rs
As
dao.Recordset
=
mydb.OpenRecordset(sql, dao.RecordsetTypeEnum.dbOpenDynaset)
'
dao.RecordsetTypeEnum.dbOpenDynaset是recordset表类型
132
If
rs.EOF
Then
133
MsgBox
(
"
这个学号不存在,如果要增加此人,请使用[新增]按钮
"
, MsgBoxStyle.OkOnly,
"
信息框
"
)
134
Exit Sub
135
Else
136
rs.Edit()
137
rs.Fields(
0
).Value
=
TextBox1.Text
138
rs.Fields(
1
).Value
=
TextBox2.Text
139
rs.Fields(
2
).Value
=
TextBox4.Text
140
rs.Fields(
3
).Value
=
TextBox3.Text
141
rs.Update()
142
143
End
If
144
End Sub
145
146
Private
Sub
Button8_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button8.Click
147
Dim
tmpStr
As
String
148
tmpStr
=
TextBox6.Text
149
Dim
sql
As
String
=
"
select * from 学生名册 where
"
&
ComboBox1.Text
&
"
='
"
&
tmpStr
&
"
'
"
150
myrs
=
mydb.OpenRecordset(sql, dao.RecordsetTypeEnum.dbOpenDynaset)
151
If
myrs.EOF
Then
152
MsgBox
(
"
查无此人
"
, MsgBoxStyle.OkOnly,
"
查询结果
"
)
153
Exit Sub
154
Else
155
FillControl()
156
'
reConnect()
157
End
If
158
End Sub
159
160
Private
Sub
Button10_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button10.Click
161
OpenDB()
162
FillControl()
163
End Sub
164
165
166
Private
Sub
退出ToolStripMenuItem_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
退出ToolStripMenuItem.Click
167
Button9.PerformClick()
168
End Sub
169
Public
Sub
displayStats(
ByVal
RecCount
As
Integer
)
170
Dim
ss
As
String
=
"
总记录数:
"
171
StatusStrip1.Items(
0
).Text
=
ss
&
CStr
(RecCount)
172
End Sub
173
174
Private
Sub
myontop_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
myontop.Click
175
If
myontop.Checked
=
False
Then
176
myontop.Checked
=
True
177
Me
.TopMost
=
True
'
设置窗口在最顶层
178
Else
179
myontop.Checked
=
False
180
Me
.TopMost
=
False
181
End
If
182
End Sub
183
184
Private
Sub
mySearchNumber_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
mySearchNumber.Click
185
ComboBox1.Text
=
"
学号
"
186
MsgBox
(
"
请先输入学号,再点击查询按钮
"
, MsgBoxStyle.OkOnly,
"
信息框
"
)
187
TextBox6.Focus()
188
End Sub
189
190
Private
Sub
mySearchTel_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
mySearchTel.Click
191
ComboBox1.Text
=
"
电话号码
"
192
MsgBox
(
"
请先输入电话号码,再点查询按钮
"
, MsgBoxStyle.OkOnly,
"
提示
"
)
193
TextBox6.Focus()
194
End Sub
195
196
Private
Sub
mySearchName_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
mySearchName.Click
197
ComboBox1.Text
=
"
姓名
"
198
MsgBox
(
"
请先输入查询的名字
"
, MsgBoxStyle.OkOnly,
"
提示
"
)
199
TextBox6.Focus()
200
End Sub
201
202
Private
Sub
cmExit_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
cmExit.Click
203
'
Button9.PerformClick()
204
Application.Exit()
205
End Sub
206
207
Private
Sub
cmAbout_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
cmAbout.Click
208
MsgBox
(
"
学生管理系统
"
&
vbCrLf
&
"
设计人: 王华杰
"
&
vbCrLf
&
"
制作: 李志
"
, MsgBoxStyle.OkCancel,
"
关于
"
)
209
End Sub
210
211
Private
Sub
btnCal_Click(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
btnCal.Click
212
MonthCalendar1.Visible
=
True
213
MonthCalendar1.Left
=
btnCal.Left
-
MonthCalendar1.Width
214
MonthCalendar1.Top
=
btnCal.Top
215
MonthCalendar1.BringToFront()
216
If
Len
(TextBox3.Text)
>
0
Then
217
MonthCalendar1.SetDate(TextBox3.Text.ToString)
218
End
If
219
End Sub
220
221
Private
Sub
MonthCalendar1_DateSelected(
ByVal
sender
As
System.Object,
ByVal
e
As
System.Windows.Forms.DateRangeEventArgs)
Handles
MonthCalendar1.DateSelected
222
TextBox3.Text
=
MonthCalendar1.SelectionRange.Start
223
MonthCalendar1.Visible
=
False
224
End Sub
225
End Class
226
程序运行截图: