文章分类 | 推荐文章 | 最新文章 | 热点文章 | 最新软件 | 精品软件 | 下载排行 | 推荐下载 | firefox | WPS | 杀毒软件 | Picasa
清风网络
首 页 软件下载 网络学院 数码学院
QQ 电脑入门 游戏 操作系统 图形图像 办公软件 媒体动画 精文荟萃 常用软件 网页编程 技术开发 网络技术 认证考试 网站建设 文章专栏
当前位置:清风网络学院网络编程数据库VB.NET的数据库基础编程(2)
精品推荐
特别推荐
·学习SQL语句之SQL语句大全
·数据备份失败的五个原因及解决办法
·解决SQL Server常见的七个经典问题
·SQL存储过程的概念,创建,调用,管理,删除,优点
·带你轻松接触13个数据库术语
·如何恢复系统数据库如何恢复系统数据库?
·通过实例讲解由浅入深学会存储过程
·学会三个范式快速成为数据库设计的高手
·SQL数据库应聘人员面试时经常被问的问题
·问答:查询分析器不能单步调试的的原因
·数据库基础知识:SQL中的IIF语句详解
·WindowsXP+IIS+PHP5+MySQL5+Zend+GD库+phpMyAdmin+PHPWind 5.3 安装教程
·你知道吗?优化数据库前的十大问题
·自动备份注意事项让你远离误删数据噩梦
·关于MSSQL数据库日志满的快速解决办法
·SQL安全设置攻略
·SQL注入的不常见方法
·关于SQL语句的优化方式
·SQL优化34条
·查询及删除重复记录的方法
热点TOP10
·学习SQL语句之SQL语句大全
·玩转Windows桌面图标
·Transact-SQL语句总汇
·了解SQL语句的功能,快速学会SQL语句
·ASP.NET中数据库的操作初步----DataSet操作数据库
·ADO连接数据库字符串大全
·精妙SQL语句收集
·101个微软提供的Visual Studio 2005示例
·Apache+php+mysql安装与配置详解
·用独立的DLL来存储图片(资源文件)
·一个简单的酒店系统的数据库设计
·通过Remoting service上传文件
·如何在GridView中一次性批量更新多行数据
·VB.NET的数据库基础编程(1)
·使用设计模式构建通用数据库访问类
·QQ聊天记录器演示程序(一)
·SQL优化34条
·数据库基础知识:SQL中的IIF语句详解
·VB.NET的数据库基础编程(2)
·SQL数据库应聘人员面试时经常被问的问题

VB.NET的数据库基础编程(2)

日期:2007年2月21日 作者:清风网络学院 查看:[大字体 中字体 小字体]


七.VB.NET数据库基本编程完整源程序代码和程序运行界面:

在掌握了上面的那些针对数据库的基本操作过以后,就可以得到用VB.NET进行数据库基本编程的源程序代码,下面代码(data.vb)的操作数据库是Access 2000,如下:

     
Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
    Inherits Form
    Private components As System.ComponentModel.Container = Nothing
    Private WithEvents lastrec As Button
    Private WithEvents nextrec As Button
    Private WithEvents previousrec As Button
    Private WithEvents firstrec As Button
    Private t_books As TextBox
    Private t_nl As TextBox
    Private t_xb As TextBox
    Private t_xm As TextBox
    Private t_id As TextBox
    Private l_books As Label
    Private l_nl As Label
    Private l_xb As Label
    Private l_xm As Label
    Private l_id As Label
    Private label1 As Label
    Private myDataSet As DataSet
    Private WithEvents button1 As Button
    Private WithEvents button2 As Button
    Private WithEvents button3 As Button
    Private WithEvents button4 As Button
    Private myBind As BindingManagerBase

    Public Sub New ( ) 
        MyBase.New ( ) 
        GetConnected ( ) 
        InitializeComponent ( ) 
    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
    Public Sub GetConnected ( ) 
        '创建一个数据连接
        Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; 
  Data Source = db.mdb "
        Dim myConn As OleDbConnection = New OleDbConnection ( ) 
        myConn.ConnectionString = strCon
        Dim strCom As String = " SELECT * FROM person "
        '创建一个 DataSet
        myDataSet = New DataSet ( ) 
        myConn.Open ( ) 
        '通过OleDbDataAdapter对象得到一个数据集
        Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter 
  ( strCom  , myConn )
        '把Dataset绑定books数据表
        myCommand.Fill ( myDataSet  , "person" )
        '关闭此数据连接
        myConn.Close ( ) 
    End Sub
    '初始化窗体中的组件
    Private Sub InitializeComponent ( ) 
        label1 = New Label ( ) 
        l_xm = New Label ( ) 
        l_books = New Label ( ) 
        t_xm = New TextBox ( ) 
        t_nl = New TextBox ( ) 
        nextrec = New Button ( ) 
        lastrec = New Button ( ) 
        firstrec = New Button ( ) 
        button1 = New Button ( ) 
        t_xb = New TextBox ( ) 
        button3 = New Button ( ) 
        button4 = New Button ( ) 
        t_books = New TextBox ( ) 
        previousrec = New Button ( ) 
        button2 = New Button ( ) 
        l_nl = New Label ( ) 
        l_xb = New Label ( ) 
        l_id = New Label ( ) 
        t_id = New TextBox ( ) 
        SuspendLayout ( ) 
 label1.Font = New Font ( "Microsoft Sans Serif" , 14! )
        label1.ForeColor = Color.Navy
        label1.Location = New Point ( 40 , 24 )
        label1.Name = "label1"
        label1.Size = New Size ( 265 , 28 )
        label1.TabIndex = 19
        label1.Text = "VB.NET数据库基础编程"
        label1.TextAlign = ContentAlignment.MiddleCenter
 l_xm.Font = New Font ( "Microsoft Sans Serif" , 10! )
        l_xm.Location = New Point ( 40 , 104 )
        l_xm.Name = "l_xm"
        l_xm.Size = New Size ( 143 , 23 )
        l_xm.TabIndex = 14
        l_xm.Text = "姓      名:"
        l_xm.TextAlign = ContentAlignment.MiddleCenter
 l_books.Font = New Font ( "Microsoft Sans Serif" , 10! )
        l_books.Location = New Point ( 40 , 200 )
        l_books.Name = "l_books"
        l_books.Size = New Size ( 143 , 24 )
        l_books.TabIndex = 16
        l_books.Text = "邮政编码:"
        l_books.TextAlign = ContentAlignment.MiddleCenter
 t_xm.Location = New Point ( 160 , 104 )
        t_xm.Name = "t_xm"
        t_xm.Size = New Size ( 104 , 21 )
        t_xm.TabIndex = 2
        t_xm.Text = ""
 t_xm.DataBindings.Add  ( New Binding  ( "Text"  , 
 myDataSet  , "person.xm" ) ) 
        t_nl.Location = New Point ( 160 , 168 )
        t_nl.Name = "t_nl"
        t_nl.Size = New Size ( 102 , 21 )
        t_nl.TabIndex = 4
        t_nl.Text = ""
 t_nl.DataBindings.Add  ( New Binding  ( "Text"  , 
 myDataSet  , "person.nl" ) ) 
 nextrec.Font = New Font ( "Microsoft Sans Serif" , 8! )
        nextrec.ForeColor = Color.Black
        nextrec.Location = New Point ( 176 , 248 )
        nextrec.Name = "nextrec"
        nextrec.Size = New Size ( 60 , 28 )
        nextrec.TabIndex = 8
        nextrec.Text = "后一条"
 lastrec.Font = New Font ( "Microsoft Sans Serif" , 8! )
        lastrec.ForeColor = Color.Black
        lastrec.Location = New Point ( 240 , 248 )
        lastrec.Name = "lastrec"
        lastrec.Size = New Size ( 60 , 28 )
        lastrec.TabIndex = 9
        lastrec.Text = "尾记录"
 firstrec.Font = New Font ( "Microsoft Sans Serif" , 8! )
        firstrec.ForeColor = Color.Black
        firstrec.Location = New Point ( 48 , 248 )
        firstrec.Name = "firstrec"
        firstrec.Size = New Size ( 60 , 28 )
        firstrec.TabIndex = 6
        firstrec.Text = "首记录"
 button1.Font = New Font ( "Microsoft Sans Serif" , 8! )
        button1.ForeColor = Color.Black
        button1.Location = New Point ( 16 , 296 )
        button1.Name = "button1"
        button1.Size = New Size ( 70 , 28 )
        button1.TabIndex = 10
        button1.Text = "新建记录"
 t_xb.Location = New Point ( 160 , 136 )
        t_xb.Name = "t_xb"
        t_xb.Size = New Size ( 104 , 21 )
        t_xb.TabIndex = 3
        t_xb.Text = ""
 t_xb.DataBindings.Add  ( New Binding  ( "Text"  , 
 myDataSet  , "person.xb" ) ) 
 button3.Font = New Font ( "Microsoft Sans Serif" , 8! )
        button3.ForeColor = Color.Black
        button3.Location = New Point ( 176 , 296 ) 
        button3.Name = "button3"
        button3.Size = New Size ( 70 , 28 )
        button3.TabIndex = 12
        button3.Text = "修改记录"
 button4.Font = New Font ( "Microsoft Sans Serif" , 8! )
        button4.ForeColor = Color.Black
        button4.Location = New Point ( 256 , 296 )
        button4.Name = "button4"
        button4.Size = New Size ( 70 , 28 )
        button4.TabIndex = 13
        button4.Text = "删除记录"
 t_books.Location = New Point ( 160 , 200 )
        t_books.Name = "t_books"
        t_books.Size = New Size ( 102 , 21 )
        t_books.TabIndex = 5
        t_books.Text = ""
 t_books.DataBindings.Add  ( New Binding  ( "Text"  , 
 myDataSet  , "person.zip" ) ) 
 previousrec.Font = New Font ( "Microsoft Sans Serif" , 8! )
        previousrec.ForeColor = Color.Black
        previousrec.Location = New Point ( 112 , 248 )
        previousrec.Name = "previousrec"
        previousrec.Size = New Size ( 60 , 28 )
        previousrec.TabIndex = 7
        previousrec.Text = "上一条"
 button2.Font = New Font ( "Microsoft Sans Serif" , 8! )
        button2.ForeColor = Color.Black
        button2.Location = New Point ( 96 , 296 )
        button2.Name = "button2"
        button2.Size = New Size ( 70 , 28 )
        button2.TabIndex = 11
        button2.Text = "插入记录"
 l_nl.Font = New Font ( "Microsoft Sans Serif" , 10! )
        l_nl.Location = New Point ( 40 , 168 )
        l_nl.Name = "l_nl"
        l_nl.Size = New Size ( 143 , 23 )
        l_nl.TabIndex = 4
        l_nl.Text = "年      龄:"
        l_nl.TextAlign = ContentAlignment.MiddleCenter
 l_xb.Font = New Font ( "Microsoft Sans Serif" , 10! )
        l_xb.Location = New Point ( 40 , 136 )
        l_xb.Name = "l_xb"
        l_xb.Size = New Size ( 143 , 23 )
        l_xb.TabIndex = 17
        l_xb.Text = "姓      别:"
        l_xb.TextAlign = ContentAlignment.MiddleCenter
 l_id.Font = New Font ( "Microsoft Sans Serif" , 10! )
        l_id.Location = New Point ( 40 , 72 )
        l_id.Name = "l_id"
        l_id.Size = New Size ( 143 , 23 )
        l_id.TabIndex = 13
        l_id.Text = "序      号:"
        l_id.TextAlign = ContentAlignment.MiddleCenter
 t_id.Enabled = False
        t_id.Location = New Point ( 160 , 72 )
        t_id.Name = "t_id"
        t_id.Size = New Size ( 102 , 21 )
        t_id.TabIndex = 1
        t_id.Text = ""
 t_id.DataBindings.Add  ( New Binding  ( "Text"  , 
 myDataSet  , "person.id" ) ) 
 Me.AutoScaleBaseSize = New Size ( 6 , 14 )
        Me.ClientSize = New Size ( 344 , 357 )
 '在窗体中加入相应的组件
 Me.Controls.Add ( button4 ) 
 Me.Controls.Add ( button3 ) 
 Me.Controls.Add ( button2 ) 
 Me.Controls.Add ( button1 ) 
 Me.Controls.Add ( lastrec ) 
 Me.Controls.Add ( nextrec ) 
 Me.Controls.Add ( previousrec ) 
 Me.Controls.Add ( firstrec ) 
 Me.Controls.Add ( t_books ) 
 Me.Controls.Add ( t_nl ) 
 Me.Controls.Add ( t_xb ) 
 Me.Controls.Add ( t_xm ) 
 Me.Controls.Add ( t_id ) 
 Me.Controls.Add ( l_books ) 
 Me.Controls.Add ( l_nl ) 
 Me.Controls.Add ( l_xb ) 
 Me.Controls.Add ( l_xm ) 
 Me.Controls.Add ( l_id ) 
 Me.Controls.Add ( label1 ) 
        Me.Name = "Form1"
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Text = "VB.NET数据库基础编程!"
        Me.ResumeLayout ( False )
 '创建BindingManagerBase对象
        myBind = Me.BindingContext  ( myDataSet  , "person" )  
    End Sub
    Private Sub button1_Click (ByVal sender As Object , _
      ByVal e As System.EventArgs) Handles button1.Click
        t_id.Text = ( myBind.Count + 1 ).ToString ( ) 
        t_xm.Text = ""
        t_xb.Text = ""
        t_nl.Text = ""
        t_books.Text = ""
    End Sub
    '插入数据记录操作代码
    Private Sub button2_Click (ByVal sender As Object , _
      ByVal e As System.EventArgs) Handles button2.Click
        '判断所有字段是否添完,添完则执行,反之弹出提示 
        If  ( t_id.Text <> "" And t_xm.Text <> "" 
   And t_xb.Text <> "" And t_nl.Text <> "" 
   And t_books.Text <> "" ) Then
            Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; 
   Data Source = db.mdb"
            Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
            myConn.Open ( ) 
            Dim strInsert As String = " INSERT INTO person  ( id  , xm  , xb  , nl
    , zip ) VALUES  ( " & _
            t_id.Text + " , '" & _
            t_xm.Text + "' , '" & _
            t_xb.Text + "' , " & _
            t_nl.Text + " , " & _
            t_books.Text + ")"
            Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
            inst.ExecuteNonQuery ( ) 
            myConn.Close ( ) 
            myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( ) 
            myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( ) 
            myDataSet.Tables ( "person" ).AcceptChanges ( ) 
        Else
            MessageBox.Show ( "必须填满所有字段值!" , "错误!" )
        End If
    End Sub
    '修改数据记录代码
    Private Sub button3_Click (ByVal sender As Object , _
      ByVal e As System.EventArgs) Handles button3.Click
        Dim i As Integer = myBind.Position
        '连接到一个数据库
        Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; 
  Data Source = db.mdb "
        Dim myConn As OleDbConnection = New OleDbConnection ( strCon )
        myConn.Open ( ) 
        myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( ) 
        '从数据库中修改指定记录
        Dim strUpdt As String = " UPDATE person SET xm = '" _
         + t_xm.Text + "'  , xb = '" _
         + t_xb.Text + "'  , nl = " _
         + t_nl.Text + "  , zip = " _
         + t_books.Text + " WHERE id = " + t_id.Text
        Dim myCommand As OleDbCommand = New OleDbCommand ( strUpdt , myConn )
        myCommand.ExecuteNonQuery ( ) 
        myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( ) 
        myDataSet.Tables ( "person" ).AcceptChanges ( ) 
        myConn.Close ( ) 
        myBind.Position = i
    End Sub
    '删除数据记录代码
    Private Sub button4_Click (ByVal sender As Object , _
      ByVal e As System.EventArgs) Handles button4.Click
     '连接到一个数据库
            Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; 
   Data Source = db.mdb "
            Dim myConn As OleDbConnection = New OleDbConnection ( strCon )
            myConn.Open ( ) 
            Dim strDele As String = "DELETE FROM person WHERE id= " + t_id.Text
            Dim myCommand As OleDbCommand = New OleDbCommand ( strDele , myConn )
            '从数据库中删除指定记录
            myCommand.ExecuteNonQuery ( ) 
            '从DataSet中删除指定记录
            myDataSet.Tables ( "person" ).Rows ( myBind.Position ).Delete ( ) 
            myDataSet.Tables ( "person" ).AcceptChanges ( ) 
            myConn.Close ( ) 
    End Sub
    '按钮"尾记录"对象事件程序 
    Private Sub lastrec_Click ( ByVal sender As Object , _
      ByVal e As System.EventArgs ) Handles lastrec.Click
        myBind.Position = myBind.Count - 1
    End Sub
    '按钮"下一条"对象事件程序 
    Private Sub nextrec_Click ( ByVal sender As Object , _
      ByVal e As System.EventArgs ) Handles nextrec.Click
        If myBind.Position = myBind.Count - 1 Then
            MessageBox.Show ( "已经到了最后一条记录!" , "信息提示!" , 
   MessageBoxButtons.OK , MessageBoxIcon.Information )
        Else
            myBind.Position = myBind.Position + 1
        End If
    End Sub
    '按钮"上一条"对象事件程序 
    Private Sub previousrec_Click ( ByVal sender As Object , _
      ByVal e As System.EventArgs ) Handles previousrec.Click
        If  ( myBind.Position = 0 ) Then
            MessageBox.Show ( "已经到了第一条记录!" , "信息提示!" , 
   MessageBoxButtons.OK , MessageBoxIcon.Information )
        Else
            myBind.Position = myBind.Position - 1
        End If
    End Sub
    '按钮"首记录"对象事件程序
    Private Sub firstrec_Click ( ByVal sender As Object , _
      ByVal e As System.EventArgs ) Handles firstrec.Click
        myBind.Position = 0
    End Sub
End Class
Module Module1
    Sub Main ( ) 
        Application.Run ( New Form1 ( ) )
    End Sub
End Module   


经过以下编译命令编译过以后:

     
vbc /r:system.dll /r:system.windows.forms.dll 
    /r:system.drawing.dll /r:system.XML.dll 
 /r:system.data.dll data.vb   


运行程序就可以得到下面的运行界面:



图01:VB.NET数据库基础编程运行界面

八:总结:

VB.NET进行数据库编程主要利用的是ADO.NET,ADO.NET是.Net FrameWork SDK中用以操作数据库的一系列类库的总称。本文介绍的这些操作虽然是最为基本的,但其实也是至关重要的,因为任何一种复杂的数据库处理都可以分解为上述这些操作,希望本文介绍的这些内容对你用VB.NET开发数据库相关程序有所帮助。

(出处:清风下载学院






上一篇:VB.NET的数据库基础编程(1)

下一篇:使用asp.net发送邮件详解

VB.NET的数据库基础编程(2) 相关文章:
·XP系统运行慢?小编自用7招解决
·C#编写的windows计算器-源代码
·Beej的网络socket编程指南
·socket编程原理
·TCP/IP编程实现远程文件传输
·编写简单的中文分词程序
·ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!
·如何用C#编写文本编辑器
·用ASP编写网上调查投票系统
·Windows下C语言网络编程快速入门
VB.NET的数据库基础编程(2) 相关软件:
·名片编辑专家V3.0 绿色特别版
·C++编程思想
·Windows环境下32位汇编语言程序设计
·功能较强的一个FLASH编辑器
·UNIX环境高级编程
·新编日语共四册mp3
·《红色警戒2》地图编辑器
·shell编程和unix命令
·Visual C++.NET深入编程电子版
·新编VB6.0 教程配套教学光盘

特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
[打印本页] [关闭窗口] 转载请注明来源:http://www.viphot.com
| 帮助(?) | 版权声明 | 友情连接 | 关于我们 | 信息发布
Copyright 2007 www.viphot.com All Rights Reserved. 鄂ICP备05000083号Powered by:vipcn