所有源代码在这里下载:
http://www.up2e.com/resource.php
用vb.net与acess结合实现简单的电话号码本程序(二)完--by zigz(LuHai) luluhai@eastday.com
四) 相关知识 拿到课程设计题目时,了解到本题和数据库相关,而.NET又引入了全新的ADO.NET数据访问技术,所以我们就去了解了些这方面的知识,看看她如何和Access结合建立数据库程序,在学习过程中发现其中需要用到SQL语句来实现对数据库的增加,查询,修改以及删除等操作。 其中OleDbConnection控件用来和数据库进行连接,OleDbCommand控件用来向数据库发送SQL命令,来完成对数据库的操作。
几个知识点如下: 1) SQL语言 A,Select语句 其主要用途是从数据表中获得一个数据集,他的语法如下: Select selectlist From tablename [Where searchCondition] B,Insert语句 用于向数据库表中插入新纪录,他的语法如下: Insert [into] tablename [(column list)] Values (default|NULL|expression) C,Delete语句 用于删除数据库中的纪录,他的语法如下: Delete From tablename [Where searchcondition]
2)ADO.NET数据对象 其中OleDbConnection控件用来和数据库进行连接,OleDbCommand控件用来向数据库发送SQL命令,来完成对数据库的操作。
五) 编程思路和关键程序 思路就是通过设置OleDbConnection控件来建立与Acess2000数据库的连接,通过设置OleDbCommand控间的CommandText属性来增加,删除纪录,通过OleDbDataReader结合ListView控件来显示数据库的所有纪录。
1) 关于添加按钮功能的实现 首先编写了一个用于添加纪录的函数addrecord()
Private Sub addrecord()
Label5.Visible = False
ErrorProvider1.SetError(homephone, "")
ErrorProvider1.SetError(mobilephone, "")
OleDbConnection1.Open() ''''打开连接
OleDbCommand1.CommandText = "insert into txl(name,mobilephone,homephone,email) values(''''" + tbname.Text + "'''',''''" + mobilephone.Text + "'''',''''" + homephone.Text + "'''',''''" + email.Text + "'''')"
OleDbCommand1.ExecuteNonQuery() ''''向数据库中添加一行纪录
tbname.Text = "" ''''清空所有
mobilephone.Text = ""P
homephone.Text = ""
email.Text = ""
MessageBox.Show("已经成功添加了一条纪录!", "info", MessageBoxButtons.OK)
OleDbConnection1.Close() ''''关闭连接
Private Sub btadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btadd.Click
If tbname.Text = "" Then ''''如果用户没有输入姓名
MessageBox.Show("对不起!请输入您的姓名。", "info", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ElseIf Not mobilephone.Text = "" Then
If Not IsNumeric(mobilephone.Text) Then
ErrorProvider1.SetError(mobilephone, "1")
Label5.Visible = True
Else
addrecord()
End If
ElseIf Not homephone.Text = "" Then
If Not IsNumeric(homephone.Text) Then
ErrorProvider1.SetError(homephone, "1")
Label5.Visible = True
Else
addrecord()
End If
Else
addrecord()
End If
2) 关于删除记录功能的实现
图7 删除纪录
程序也主要就是通过设置OleDbConnection控件来建立与Acess2000数据库的连接,通过设置OleDbCommand控间的CommandText属性来实现删除纪录的功能。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If tbdel.Text = "" Then
MessageBox.Show("你还没有输入姓名!", "info", MessageBoxButtons.OK)
Else
Dim mainform As New Form1()
mainform.OleDbCommand1.CommandText = "delete from txl where name=''''" + tbdel.Text + "''''"
mainform.OleDbConnection1.Open()
mainform.OleDbCommand1.ExecuteNonQuery()
MessageBox.Show("已经删除成功!", "info", MessageBoxButtons.OK)
  [1] [2] 下一页 [VB.NET程序]用vb.net与acess结合实现简单的电话号码本程序(一…
|