- ©«¤l
- 4901
- ¥DÃD
- 44
- ºëµØ
- 24
- ¿n¤À
- 4916
- ÂI¦W
- 46
- §@·~¨t²Î
- Windows 7
- ³nÅ骩¥»
- Office 20xx
- ¾\ŪÅv
- 150
- ©Ê§O
- ¨k
- ¨Ó¦Û
- ¥x¥_
- µù¥U®É¶¡
- 2010-4-30
- ³Ì«áµn¿ý
- 2025-2-22
               
|
[Âà¶K] ADO - ¦s¨úOracle¸ê®Æªº½d¨Ò
¥Ñ©ó¤u§@¤Wªº»Ýn, ²`¤ÁÅé·|ADOªº¤è«K. ¨ä¹ê»PDatabase¤§¶¡ªº¸ê®Æ¦s¨ú¡B³z¹LExcel¦A³B²z... ³o¨Ç¨Æ, ¤×¨ä¹ï©óIT¤Hû, ³£¦³¨ä¥L¤èªk©M¤u¨ã¥i¥H¹F¦¨, ¦ý¬Û¹ï¨Ó»¡ÁÙ¬O³Â·Ð.
¥H¤U½d¨Ò¬O®Ú¾Ú§Ú¥®ÉªºÀô¹Ò (DB, ODBC), ¦pªG¦U¦ì¨Ï¥Î¦³¦ó°ÝÃD, Åwªï´£¥X°Q½×
'ADO - Query Oracle from Excel via ODBC driver
Sub ADO_example()
¡@Dim con As Object, com As Object, rec As Object
¡@Set con = CreateObject("ADODB.Connection")
¡@Set com = CreateObject("ADODB.Command")
¡@Set rec = CreateObject("ADODB.Recordset")
¡@On Error GoTo ADO_Err
¡@'¬P¸¹³¡¤À¨Ì¹ê»Ú³]©w¦Û¦æקï
¡@con.Open "DRIVER={Microsoft ODBC for Oracle};UID=*** WD=***;SERVER=*****;"
¡@With com
¡@¡@¡@.ActiveConnection = con
¡@¡@¡@.CommandType = 1 '1=adCmdText 4=adCmdStoredProc
¡@¡@¡@.CommandText = "select emp_no,emp_name from employee where dept_no='001'"
¡@End With
¡@Set rec = com.Execute
¡@'¾ãӽƻs¨ì¤u§@ªí
¡@Cells(1, 1).CopyFromRecordset rec
¡@'Ū¨úÄæ¦W¤è¦¡¤@
¡@For Each f In rec.Fields
¡@¡@¡@Debug.Print f.Name
¡@Next
¡@'Ū¨úÄæ¦W¤è¦¡¤G¡@PS. rec(0)¬° rec.Fields(0) ¤§Â²¤Æ¼gªk
¡@Debug.Print rec(0).Name & ", " & rec(1).Name
¡@'ª½±µÅª¨úRecordset¸ê®Æ
¡@While Not rec.EOF 'Loop: ¾A¥Î¦hµ§
¡@¡@ Debug.Print rec(0) & ", " & rec(1)
¡@¡@ rec.MoveNext
¡@Wend
¡@rec.Close: con.Close
1 Set con = Nothing: Set com = Nothing: Set rec = Nothing
¡@Exit Sub
ADO_Err:
¡@MsgBox Err & vbLf & Err.Description, 16
¡@GoTo 1
End Sub
¥t¥H¤U¬OConnection©MRecordsetª«¥óªº¤@¨ÇÄÝ©Ê, ¥i¥Î©ó´ú¸Õ, ¤]¤@¨Ö¦C¥X´£¨Ñ°Ñ¦Ò --
'Test Oracle Connection info after Openning
¡@'Debug.Print "con.CursorLocation¡@=" & con.CursorLocation¡@'2=adUseServer 3=adUseClient
¡@'Debug.Print "con.DefaultDatabase =" & con.DefaultDatabase 'not supported by Oracle
¡@'Debug.Print "con.IsolationLevel¡@=" & con.IsolationLevel
¡@'Debug.Print "con.Mode¡@¡@¡@¡@¡@¡@=" & con.Mode
¡@'Debug.Print "con.Provider¡@¡@¡@¡@=" & con.Provider
¡@'Debug.Print "con.State¡@¡@¡@¡@¡@ =" & con.State
¡@'Debug.Print "con.Version¡@¡@¡@¡@ =" & con.Version
'Test Recordset info after Executing of Command
¡@'Debug.Print "rec.AbsolutePosition=" & rec.AbsolutePosition '-2=adPosBOF -3=adPosEOF -1=adPosUnknown
¡@'Debug.Print "rec.ActiveCommand¡@ =" & rec.ActiveCommand.CommandText
¡@'Debug.Print "rec.ActiveConnection=" & rec.ActiveConnection.ConnectionString
¡@'Debug.Print "rec.CursorLocation¡@=" & rec.CursorLocation '2=adUseServer 3=adUseClient
¡@'Debug.Print "rec.CursorType¡@¡@¡@=" & rec.CursorType '0=adOpenForwardOnly 1=adOpenKeyset 2=adOpenDynamic 3=adOpenStatic
¡@'Debug.Print "rec.DataMember¡@¡@¡@=" & rec.DataMember
¡@'Debug.Print "rec.Fields.Count¡@¡@=" & rec.Fields.Count
¡@'Debug.Print "rec.Index¡@¡@¡@¡@¡@ =" & rec.Index
¡@'Debug.Print "rec.MaxRecords¡@¡@¡@=" & rec.MaxRecords
¡@'Debug.Print "rec.PageCount¡@¡@¡@ =" & rec.PageCount
¡@'Debug.Print "rec.RecordCount¡@¡@ =" & rec.RecordCount
¡@'Debug.Print "rec.State¡@¡@¡@¡@¡@ =" & rec.State
¡@'Debug.Print "rec.Status¡@¡@¡@¡@¡@=" & rec.Status |
|