'Ctrl+t找Microsoft DataGrid Control....
創(chuàng)新互聯(lián)建站專注于鳳慶網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供鳳慶營銷型網(wǎng)站建設(shè),鳳慶網(wǎng)站制作、鳳慶網(wǎng)頁設(shè)計(jì)、鳳慶網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造鳳慶網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供鳳慶網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
'菜單"工程"-"引用" 找 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 某個(gè)表"
rst.CursorLocation = adUseClient
rst.Open SqlStr, cn, adOpenDynamic, adLockOptimistic, adCmdText '打開記錄集
Set DataGrid1.DataSource = rst '給DataGrid修改數(shù)據(jù)源
老問題了,隨便上網(wǎng)就找得到,
以下供參考,轉(zhuǎn)自ccid。
首先要安裝ORACLE客戶端或服務(wù)器端,建好DB以及SERVICE,連接成功后就可以開始了,以下是VB訪問ORACLE的代碼:
Option Explicit
'以下是一些變量以及參數(shù)的定義
'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的函數(shù)
'連接成功: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語句從數(shù)據(jù)庫的表里取得記錄
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
'執(zhí)行更新語句,返回受本次更新影響的記錄數(shù)
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
將以上代碼放入模塊,直接調(diào)用即可.
這個(gè)問題,很多人反應(yīng)過,應(yīng)該是win8不支持vb6.
如果你的電腦很好的話,你可以裝個(gè)虛擬機(jī)vmare軟件,里面裝xp和vb6(相當(dāng)于一臺已啟動的電腦)。現(xiàn)在很多人用虛擬機(jī)軟件,很好用。
把vb安裝后的文件,從C:\Program Files(x86)拷貝到C:\Program Files,
執(zhí)行C:\Program Files里的vb,這樣調(diào)試的時(shí)候能連接oracle。
ps:vb安裝在C:\Program Files(x86),編譯后的程序能執(zhí)行,只是調(diào)試受影響,安裝vb時(shí)只能在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數(shù)據(jù)訪問控件的屬性頁進(jìn)行連接Oracle數(shù)據(jù)庫。
1)建立數(shù)據(jù)工程,在窗體添加Adodc數(shù)據(jù)訪問控件。鼠標(biāo)選中Adodc控件-右鍵-Adodc屬性-使用連接字符串-生成。
2)選Microsoft OLE DB Provider For Oracle,下一步。
3)根據(jù)數(shù)據(jù)鏈接屬性對話框填寫所需內(nèi)容,測試連接。點(diǎn)確定,最終在Adodc屬性頁獲得連接字符串,復(fù)制該字符串,即可作為Adodc數(shù)據(jù)訪問控件直接使用,或在代碼中用于Adodc控件或ADO數(shù)據(jù)訪問對象。
名稱欄目:vb6如何讀oracle,vb怎么念
當(dāng)前地址:http://vcdvsql.cn/article36/hssgsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、ChatGPT、網(wǎng)站改版、定制開發(fā)、品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)