第一: 导入空间imports System.Data.SqlCilent
第二: 定义封装
dim con as SqlConnection
dim com as SqlCommand
dim dr as SqlDataReader
第三: 定义连接方式与加载数据库
dim str as string="Integrated Security=false;uid=sa;database=VBTest;server=10.4.7.20"
第四: 实现连接
con = New SqlConnection(str)
com = New SqlCommand("select * from 人员表", con)
con.Open()
dr = com.ExecuteReader
While dr.Read
MsgBox(dr.GetString(0))
End While
dr.Close()
con.Close()
SQL Server2000 数据库本地验证方式
第一: 导入空间imports System.Data.SqlCilent
第二: 定义封装
dim con as SqlConnection
dim com as SqlCommand
dim dr as SqlDataReader
第三: 定义连接方式与加载数据库
Dim str As String = "database=VBTest;server=10.4.7.20;Integrated Security=true"
第四: 实现连接
con = New SqlConnection(str)
com = New SqlCommand("select * from 人员表", con)
con.Open()
dr = com.ExecuteReader
While dr.Read() '读数据
MsgBox(dr(0))
End While
dr.Close()
con.Close()
Access 数据库带密码验证方式
第一: 导入空间imports System.Data.OleDb
第二: 定义封装
Dim acon As OleDbConnection
Dim acom As OleDbCommand
Dim adr As OleDbDataReader
第三: 定义连接方式与加载数据库
Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:VBTest.mdb; Jet OLEDB:Database Password=123"
第四: 实现连接
acon = New OleDbConnection(str)
acom = New OleDbCommand("select * from 人员表", acon)
Try
acon.Open()
MsgBox("Ok")
adr = acom.ExecuteReader
While adr.Read
MsgBox(adr(0))
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
adr.Close()
acon.Close()
Access 数据库不带密码验证方式
第一: 导入空间imports System.Data.OleDb
第二: 定义封装
Dim acon As OleDbConnection
Dim acom As OleDbCommand
Dim adr As OleDbDataReader
第三: 定义连接方式与加载数据库
Dim str As String = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:VBTest1.mdb"
第四: 实现连接
acon = New OleDbConnection(str)
acom = New OleDbCommand("select * from 人员表", acon)
Try
acon.Open()
MsgBox("Ok")
adr = acom.ExecuteReader
While adr.Read
MsgBox(adr(0))
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
adr.Close()
acon.Close() 作者: ♂张耕明 时间: 2012-10-23 14:13