'Ctrl+t找Microsoft DataGrid Control....
创新互联建站专注于凤庆网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供凤庆营销型网站建设,凤庆网站制作、凤庆网页设计、凤庆网站官网定制、重庆小程序开发服务,打造凤庆网络公司原创品牌,更为您提供凤庆网站排名全网营销落地服务。
'菜单"工程"-"引用" 找 Microsoft ActiveX Data Object....
Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SqlStr As String
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;Integrated Security=no;"
SqlStr = "Select * From 某个表"
rst.CursorLocation = adUseClient
rst.Open SqlStr, cn, adOpenDynamic, adLockOptimistic, adCmdText '打开记录集
Set DataGrid1.DataSource = rst '给DataGrid修改数据源
老问题了,随便上网就找得到,
以下供参考,转自ccid。
首先要安装ORACLE客户端或服务器端,建好DB以及SERVICE,连接成功后就可以开始了,以下是VB访问ORACLE的代码:
Option Explicit
'以下是一些变量以及参数的定义
'DB的名称
Public Const strdbname = "DBNAME"
'用户名
Public Const strusr = "BLUESKY35"
'密码
Public Const strpwd = "PASSWORD"
'OraSession
Public OraSession As Object
'OraDatabase
Public OraDB As OraDatabase
'OraDynaset
Public OraDyn As OraDynaset
'连接ORACLE的函数
'连接成功:true 连接失败:false
Public Function f_connect() As Boolean
On Error GoTo ierr
f_connect = False
Set OraSession = CreateObject("OracleInProcServer.XOraSession")
Set OraDB = OraSession.OpenDatabase(strdbname, strusr "/" strpwd, 0)
f_connect = True
Exit Function
ierr:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
f_connect = False
End Function
'通过传入SQL语句从数据库的表里取得记录
Public Function f_select(strsql As String) As OraDynaset
On Error GoTo ierr
Set OraDyn = OraDB.CreateDynaset(strsql, ORADYN_READONLY)
Set f_select = OraDyn
Exit Function
ierr:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
Set f_select = Nothing
End Function
'执行更新语句,返回受本次更新影响的记录数
Public Function f_execute(strsql As String) As Boolean
On Error GoTo ierr
f_execute = False
OraDB.BeginTrans
f_execute = OraDB.ExecuteSQL(strsql)
Exit Function
ierr:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
OraDB.Rollback
End Function
'断开连接
'断开成功:true 断开失败:false
Public Function f_disconnect() As Boolean
On Error GoTo ierr
f_disconnect = False
If Not OraDyn Is Nothing Then
OraDyn.Close
End If
Set OraSession = Nothing
OraDB.Close
Set OraDB = Nothing
f_disconnect = True
Exit Function
ierr:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error"
f_disconnect = False
End Function
将以上代码放入模块,直接调用即可.
这个问题,很多人反应过,应该是win8不支持vb6.
如果你的电脑很好的话,你可以装个虚拟机vmare软件,里面装xp和vb6(相当于一台已启动的电脑)。现在很多人用虚拟机软件,很好用。
把vb安装后的文件,从C:\Program Files(x86)拷贝到C:\Program Files,
执行C:\Program Files里的vb,这样调试的时候能连接oracle。
ps:vb安装在C:\Program Files(x86),编译后的程序能执行,只是调试受影响,安装vb时只能在Program Files(x86)里,不能修改。
Option Explicit
Public rn As ADODB.Connection
Public Function CreateDataSource(DataSource As String, UserID As String, Password As String) As Boolean
On Error GoTo DbConErr:
Set rn = New ADODB.Connection
With rn
.ConnectionString = "Provider=OraOLEDB.Oracle.1; password=TEST ;User ID = TEST;Data Source=DEV;Locale Identifier=2052"
.Open
End With
CreateDataSource = True
Exit Function
DbConErr:
CreateDataSource = False
End Function
Sub BlobToFile(fld As ADODB.Field, filename As String, Optional ChunkSize As Long = 8192)
Dim fnum As Integer, bytesleft As Long, bytes As Long
Dim tmp() As Byte
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "field doesn't support the GetChunk method."
End If
If Dir$(filename) "" Then Kill filename
fnum = FreeFile
Open filename For Binary As fnum
bytesleft = fld.ActualSize
Do While bytesleft
bytes = bytesleft
If bytes ChunkSize Then bytes = ChunkSize
tmp = fld.GetChunk(bytes)
Put #fnum, , tmp
bytesleft = bytesleft - bytes
Loop
Close #fnum
End Sub
Sub FileToBlob(fld As ADODB.Field, filename As String, Optional ChunkSize As Long = 8192)
Dim fnum As Integer, bytesleft As Long, bytes As Long
Dim tmp() As Byte
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "field doesn't support the GetChunk method."
End If
If Dir$(filename) = "" Then Err.Raise 53, , "File not found"
fnum = FreeFile
Open filename For Binary As fnum
bytesleft = LOF(fnum)
Do While bytesleft
bytes = bytesleft
If bytes ChunkSize Then bytes = ChunkSize
ReDim tmp(1 To bytes) As Byte
Get fnum, , tmp
fld.AppendChunk tmp
bytesleft = bytesleft - bytes
Loop
Close #fnum
End Sub
Private Sub cmdread_Click()
Dim rs As New ADODB.Recordset
rs.ActiveConnection = rn
rs.LockType = adLockOptimistic
rs.CursorLocation = adUseClient
rs.Source = "select * from t_demo"
rs.Open
comDlgDir.DialogTitle = "保存文件"
comDlgDir.Filter = "*.*"
comDlgDir.ShowSave
Call BlobToFile(rs.Fields("text"), comDlgDir.filename)
Set rs = Nothing
Exit Sub
Set rs = Nothing
End Sub
Private Sub cmdsave_Click()
Dim rs As New ADODB.Recordset
rs.ActiveConnection = rn
rs.LockType = adLockOptimistic
rs.CursorLocation = adUseClient
rs.Source = "select * from t_demo"
rs.Open
rs.AddNew
comDlgDir.DialogTitle = "选取文件"
comDlgDir.ShowOpen
rs.Fields("id").Value = 1
If comDlgDir.filename "" Then
Call FileToBlob(rs.Fields("text"), comDlgDir.filename)
rs.Update
End If
Set rs = Nothing
Exit Sub
Set rs = Nothing
End Sub
Private Sub Form_Load()
If Not CreateDataSource("DEV", "TEST", "TEST") Then
MsgBox "Connection failure!"
End If
End Sub
VB6.0可通过Adodc数据访问控件的属性页进行连接Oracle数据库。
1)建立数据工程,在窗体添加Adodc数据访问控件。鼠标选中Adodc控件-右键-Adodc属性-使用连接字符串-生成。
2)选Microsoft OLE DB Provider For Oracle,下一步。
3)根据数据链接属性对话框填写所需内容,测试连接。点确定,最终在Adodc属性页获得连接字符串,复制该字符串,即可作为Adodc数据访问控件直接使用,或在代码中用于Adodc控件或ADO数据访问对象。