用vb.net与acess结合实现简单的电话号码本程序(二)完

所有源代码在这里下载:

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() '关闭连接

    End Sub

然后在“添加”按钮单击事件函数中:

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

End Sub

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)

            Me.Close()

        End If

End Sub

 

3)关于列表显示的代码

Private Sub showall_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim mainform As New Form1()

        Dim reader As OleDb.OleDbDataReader

        mainform.OleDbCommand1.CommandText = "select * from txl"

        mainform.OleDbConnection1.Open()

        reader = mainform.OleDbCommand1.ExecuteReader '返回所有纪录

        Do While reader.Read 'listview列出所有纪录

            Dim li As New ListViewItem()

            li.SubItems.Clear()

            li.SubItems(0).Text = reader("name").ToString

            li.SubItems.Add(reader("mobilephone").ToString)

            li.SubItems.Add(reader("homephone").ToString)

            li.SubItems.Add(reader("email").ToString)

            ListView1.Items.Add(li)

        Loop

        reader.Close()

        mainform.OleDbConnection1.Close() '关闭连接

    End Sub

 

至此,关键代码就这么多,详细代码请看我原文件。

总结:

我这个程序看上去很小,但实际上功能不少,虽然没有把查询纪录的功能做进去,但那其实与增加,删除纪录的模式完全一样,就是需要换成不同的SQL语句即可。

 

全文完。

 

by zigz(LuHai)

luluhai@eastday.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值