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查询语言多写多用就会熟练,以上供提问者参考。