人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

VB.NET的數據庫基礎編程(2)

VB.NET的數據庫基礎編程(2)

更新時間:2022-09-09 文章作者:未知 信息來源:網絡 閱讀次數:


七.VB.NET數據庫基本編程完整源程序代碼和程序運行界面:

在掌握了上面的那些針對數據庫的基本操作過以后,就可以得到用VB.NET進行數據庫基本編程的源程序代碼,下面代碼(data.vb)的操作數據庫是Access 2000,如下:

Imports SystemImports System.DrawingImports System.ComponentModelImports System.Windows.FormsImports System.Data.OleDbImports System.DataPublic 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 SubEnd ClassModule Module1 Sub Main ( ) Application.Run ( New Form1 ( ) ) End SubEnd 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開發數據庫相關程序有所幫助。(出處:賽迪網)

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 东城区| 常州市| 赤壁市| 高密市| 万山特区| 塔河县| 曲阜市| 栖霞市| 德州市| 东乌珠穆沁旗| 诸城市| 景泰县| 定安县| 泰来县| 基隆市| 连州市| 芮城县| 天等县| 桓仁| 民和| 福建省| 天柱县| 鄂温| 北票市| 宁津县| 福清市| 温州市| 五河县| 若尔盖县| 宁波市| 通山县| 湘乡市| 永福县| 左贡县| 五指山市| 稷山县| 察隅县| 德庆县| 外汇| 积石山| 上杭县|