vb資料庫開發實例
Ⅰ VB 資料庫實例
用ADO法(要先引用)
連接:
Dim cn As ADODB.Connection
Set cn=New ADODB.Connection '上兩句為聲明創建ADO變數(下同)
cn.open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=X;" 'X為資料庫路徑(加文件名)
cn.Close '關閉資料庫
Set cn=Nothing 『釋放數據
讀取:
Dim rs As ADODB.RecordSet
Set rs=New ADODB.RecordSet
rs.Open "select * from X1", cn,X2,X3 'X1為表名,cn就是上面連接的cn(下同),X2為資料庫游標類型,X3為資料庫鎖方式(讀取方式)
Do While Not rs.EOF
XXXXX '讀取數據至程序的代碼,自編
rs.MoveNext 』移動到下一條記錄
Loop
……
……
…… '關閉…,釋放…
Set rs=Nothing
Set cn=Nothing
剩下的網上搜,大把
Ⅱ 求一個用VB.net操作access資料庫的常式
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 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
Ⅲ 怎麼通過VB設計一款資料庫軟體
資料庫用VB6編程語言,對新手而言,如何用好ADODC控制項和DATAGRID控制項相當重要。
具體做法:
新建數據工程,此時VB6集成調試環境左邊工具箱內已載入了有關資料庫編程必須的控制項和引用。
然後在FORM1窗體中添加ADODC控制項和DATAGRID控制項,將DATAGRID1的屬性DATASOURCE選ADODC1,打開ADODC1控制項屬性頁使用連接字元串,選生成,在提供者選項中選MICROSOFT jet 4.0 OLE DB Provider,然後按要求連接資料庫等。在ADODC1控制項屬性頁使用連接字元串空白文本窗口中就有一長串字元串,注意該字元串可復制到程序代碼用於編程。ADODC1控制項屬性頁的數據源內有命令文本(SQL)編寫窗口可編寫SQL查詢語言。該窗口的SQL語句可復制到程序代碼用於編程。
SQL查詢語言主要結構為:
Select 查詢欄位 from 表名 Where 查詢條件語句 [排序語句或分組語句]
查詢欄位必須分別用(西文)逗號分開或就用一個*號代替,上述查詢中排序語句建議最好應用。
SQL查詢語言如有錯程序運行時告訴你出錯,作相應改動即可。
一般先按上述連接,SQL用"SELECT * FROM TabelName"作調試,無問題,用一個按鈕控制項將前述需復制的連接字元串和SQL查詢語言先復制於按鈕控制項的CLICK事件中備著,以便放著以後使用。然後刪去ADODC1控制項,再添加ADODC1控制項再添其他內容。
SQL查詢語言多寫多用就會熟練,以上供提問者參考。