VB °æ (¾«»ªÇø)
open: Permission denied
·¢ÐÅÈË: zxfsnow (±ðÅÂÎÒÉËÐÄ), ÐÅÇø: VB
±ê Ìâ: ÔÚVBÖÐÖ±½ÓÓÃODBC API·ÃÎÊÊý¾Ý¿â(תÔØ)
·¢ÐÅÕ¾: ¹þ¹¤´ó×϶¡Ïã (2000Äê06ÔÂ01ÈÕ19:11:07 ÐÇÆÚËÄ), תÐÅ
·¢ÐÅÈË: hyhour (time), ÐÅÇø: VB
±ê Ìâ: ÔÚVBÖÐÖ±½ÓÓÃODBC API·ÃÎÊÊý¾Ý¿â(תÔØ)
·¢ÐÅÕ¾: Òûˮ˼Դվ (Wed Jul 28 10:54:08 1999) , Õ¾ÄÚÐżþ
ÔÚVBÖÐÖ±½ÓÓÃODBC API·ÃÎÊÊý¾Ý¿â
Öйúº½¿ÕÐÅÏ¢ÖÐÐÄ Îâ±ó ¼ÆËã»úÊÀ½ç 19980608
¿ª·ÅÊý¾Ý¿â»¥Á¬(ODBC)ÒѾ³ÉΪClient/ServerÊý¾Ý¿âÓ¦ÓÃϵͳÖзÃÎÊÔ¶³ÌÊý¾Ý¿âµÄÒ»¸ö
±ê×× ¡£×÷Ϊǿ´óµ Ç°¶Ë¿ª·¢¹¤¾ß,Visual BasicΪ¿ª·¢ÕßÌṩÁ˶àÖÖ·ÃÎÊODBCÊý¾ÝÔ´µÄ
;¾¶,ÈçJETÊý¾Ý¿âÒýÇæ¡¢ODBC APIº¯Êý¡¢RDO½Ó¿ÚµÈ¡£±È½Ï¶øÑÔ,Ö±½ÓʹÓÃODBC
×¼¡£×÷Ϊǿ´óµÄÇ°¶Ë¿ª·¢¹¤¾ß,Visual BasicΪ¿ª·¢ÕßÌṩÁ˶àÖÖ·ÃÎÊODBCÊý¾ÝÔ´µÄ;¾¶
,Èç
JETÊý¾Ý¿âÒýÇæ¡¢ODBC APIº¯Êý¡¢RDO½Ó¿ÚµÈ¡£±È½Ï¶øÑÔ,Ö±½ÓʹÓÃODBC
ODBC APIº¯ÊýµÄÉùÃ÷·½·¨
ÓëʹÓÃÆäËü¶¯Ì¬¿âº¯ÊýÒ»Ñù,ÔÚVBÖÐʹÓÃODBC APIº¯Êý֮ǰ,±ØÐëÊÂÏÈÉùÃ÷½«ÒªÊ¹ÓÃ
µÄº¯Êý¡¢³£Á¿ºÍÊý¾Ý½á¹¹¡£ODBC APIº¯ÊýפÁôÔÚODBCÔËÐж¯Ì¬¿âODBC.DLL(16λ)»òODBC
32.DLL(32λ)ÖÐ,¸Ã¶¯Ì¬¿âλÓÚWindows×ÓĿ¼systemÖС£Í¨³£×ö·¨ÊÇÔÚVBÏîÄ¿Öе¥¶Àʹ
ÓÃÒ»¸öÄ£¿éÎļþ,È»ºó½«ODBC APIÉùÃ÷Óï¾ä¼ÓÈëÆäÖÐ,ÏÂÃæ¾ÍÊDZ¾ÎÄʵÀýÖÐʹÓõÄÄ£¿éÎÄ
¼þmodule1.basµÄÄÚÈÝ:
Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal henv&, phdbc&) As I
ntegger
Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal hdbc&, phstmt&) AsInte
ger
Declare Function SQLConnect Lib "odbc32.dll" (ByVal hdbc&, ByVal szDSN$,ByVa
l cb
SN%, ByVal szUID$, ByVal cbUID%, ByVal szAuthStr$, ByVal cbAuthStr%) As Inte
ger
Declare Function SQLColAttributesString Lib "odbc32.dll" Alias "SQLColAttrib
utes
(ByVal hstmt&, ByVal icol%, ByVal fDescType%, ByVal rgbDesc As String, ByVa
l cb
escMax%, pcbDesc%, pfDesc&) As Integer
Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal hdbc&) As Integer
Declare Function SQLExecDirect Lib "odbc32.dll" (ByVal hstmt&, ByVal szSqlSt
r$,
yVal cbSqlStr&) As Integer
Declare Function SQLFetch Lib "odbc32.dll" (ByVal hstmt&) As Integer
Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal hdbc&) As Integer
Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal henv&) As Integer
Declare Function SQLFreeStmt Lib "odbc32.dll" (ByVal hstmt&, ByVal fOption%)
As
nteger
Declare Function SQLGetData Lib "odbc32.dll" (ByVal hstmt&, ByVal icol%,ByVa
l fC
ype%, ByVal rgbValue As String, ByVal cbValueMax&, pcbValue&) As Integer
Declare Function SQLNumResultCols Lib "odbc32.dll" (ByVal hstmt&, pccol%) As
Int
ger
Global Const SQL_C_CHAR As Long = 1
Global Const SQL_COLUMN_LABEL As Long = 18
Global Const SQL_DROP As Long = 1
Global Const SQL_ERROR As Long = -1
Global Const SQL_NO_DATA_FOUND As Long = 100
Global Const SQL_SUCCESS As Long = 0
ÐèҪ˵Ã÷µÄÊÇ,ÔÚº¯ÊýÉùÃ÷ʱ,Ó¦¸Ã¸ù¾Ý³ÌÐòµÄÔËÐл·¾³Ñ¡ÔñÏàÓ¦µÄ¶¯Ì¬¿â¡£ÔÚVB×Ó
Ŀ¼samples\remauto\db_odbcÖÐÓÐÁ½¸öÎı¾ÎļþODBC16.TXTºÍODBC32.TXT,·Ö±ð´æÓÐËù
ÓÐ16λºÍ32λODBC APIº¯Êý¡¢³£Á¿ºÍÊý¾Ý½á¹¹µÄÉùÃ÷Óï¾ä,±à³Ìʱ¿ÉÒÔ´ÓÖп½±´ËùÐèµÄÉù
Ã÷Óï¾ä¡£
ʹÓÃODBC APIµÄ±à³Ì·½·¨
ÔÚVBÖе÷ÓÃODBC APIº¯Êý·ÃÎÊODBCÊý¾Ý¿â,´úÂë±àдһ°ã°´ÏÂÁйý³Ì½øÐÐ:
1.³õʼ»¯ODBC
ÔÚÕâ¸ö¹ý³ÌÖÐ,Ó¦ÓóÌÐò½«Í¨¹ýµ÷ÓÃSQLAlloEnvº¯Êý³õʼ»¯ODBC½Ó¿Ú,»ñÈ¡ODBC»·¾³¾ä±ú
¡£OD
C»·¾³¾ä±úÊÇÆäËüËùÓÐODBC×ÊÔ´¾ä±úµÄ¸¸¾ä±ú,Òò´ËÎÞÂÛ³ÌÐò½«½¨Á¢¶àÉÙ¸öODBCÁ¬½Ó,Õâ¸ö
¹ý³
Ö»ÐèÖ´ÐÐÒ»´Î¼´¿É¡£ÀýÈç:
Dim rc As Integer 'ODBCº¯ÊýµÄ·µ»ØÂë
Dim henv As Long 'ODBC»·¾³¾ä±ú
rc = SQLAllocEnv(henv) '»ñÈ¡ODBC»·¾³¾ä±ú
2.ÓëODBCÊý¾ÝÔ´½¨Á¢Á¬½Ó
Õâ¸ö¹ý³ÌÓÉÏÂÁÐÁ½¸ö²½Öè×é³É:
*Sµ÷ÓÃSQLAllocConnectº¯Êý»ñÈ¡Á¬½Ó¾ä±ú¡£ÀýÈç:
Dim hdbc As Long 'Á¬½Ó¾ä±ú
rc = SQLAllocConnect(henv, hdbc) '»ñÈ¡Á¬½Ó¾ä±ú
*S½¨Á¢Á¬½Ó¡£Õâ¸ö²½Öè¿ÉÒÔͨ¹ý¶àÖÖ·½·¨ÊµÏÖ,×î¼òµ¥Ö±¹ÛµÄ·½·¨Êǵ÷ÓÃSQLConnectº¯Êý
¡£À
Èç:
Dim DSN As String, UID As String, PWD As String
DSN = "DataSourceName" 'ODBCÊý¾ÝÔ´Ãû³Æ
UID = "UserID"
'Óû§ÕʺÅ
PWD = "Password"
'Óû§¿ÚÁî
rc = SQLConnect(hdbc, DSN, Len(DSN), UID, Len(UID), PWD, Len(PWD)) '½¨Á¢
Á¬½Ó
3.´æÈ¡Êý¾Ý
Óû§¶ÔODBCÊý¾ÝÔ´µÄ´æÈ¡²Ù×÷,¶¼ÊÇͨ¹ýSQLÓï¾äʵÏֵġ£ÔÚÕâ¸ö¹ý³ÌÖÐ,Ó¦ÓóÌÐò½«Í¨¹ý
Á¬½
ÏòODBCÊý¾Ý¿âÌá½»SQLÓï¾ä,Íê³ÉÓû§ÇëÇóµÄ²Ù×÷,¾ßÌå²½ÖèÈçÏÂ:
*Sµ÷ÓÃSQLAllocStmtº¯Êý»ñÈ¡Óï¾ä¾ä±ú,ÀýÈç:
Dim hstmt As Long
rc = SQLAllocStmt(hdbc, hstmt)
*SÖ´ÐÐSQLÓï¾ä¡£Ö´ÐÐSQLÓï¾äµÄ·½·¨±È½Ï¶à,×î¼òµ¥Ã÷Á˵ķ½·¨Êǵ÷ÓÃSQLAllocStmtº¯Êý
,Àý
ç:
Dim SQLstmt As String
SQLstmt = "SELECT * FROM authors"
rc = SQLExecDirect(hstmt, SQLstmt, Len(SQLstmt))
4.¼ìË÷½á¹û¼¯
Èç¹ûSQLÓï¾ä˳ÀûÌá½»²¢ÕýÈ·Ö´ÐÐ,ÄÇô¾Í»á²úÉúÒ»¸ö½á¹û¼¯¡£¼ìË÷½á¹û¼¯µÄ·½·¨ºÜ¶à,×î
¼òµ
¡¢×îÖ±½ÓµÄ·½·¨Êǵ÷ÓÃSQLFetchºÍSQLGetDataº¯Êý¡£SQLFetchº¯ÊýµÄ¹¦ÄÜÊǽ«½á¹û
¼¯µÄµ±Ç°
ǼָÕëÒÆÖÁÏÂÒ ¸ö¼Ç¼,SQLGetDataº¯ÊýµÄ¹¦ÄÜÊÇÌáÈ¡½á¹û¼¯Öе±Ç°¼Ç¼µÄij¸ö×Ö¶ÎÖµ
¡£Í¨
£¿ ÒÔ²ÉÓÃÒ»
¸öÑ»·ÌáÈ¡½á¹û¼¯ÖÐËùÓмǼµÄËùÓÐ×Ö¶ÎÖµ,¸ÃÑ»·Öظ´Ö´ÐÐSQLFetchºÍSQLGetDataº¯Êý
,Ö±
ÁSQLFetchº¯Êý·µ»ØSQL_NO_DATA_FOUND,Õâ±íʾÒѾµ½´ï½á¹û¼¯µÄĩβ¡£
Dim ColVal As String * 225
ColVal = String(255, 0)
Do Until SQLFetch(hstmt) = SQL_NO_DATA_FOUND
rc = SQLGetData(hstmt, i, SQL_C_CHAR, ColVal, Len(ColVal), SQL_NULL_DATA
Loop
5.½áÊøÓ¦ÓóÌÐò
ÔÚÓ¦ÓóÌÐòÍê³ÉÊý¾Ý¿â²Ù×÷¡¢Í˳öÔËÐÐ֮ǰ,±ØÐëÊͷųÌÐòÖÐʹÓõÄϵͳ×ÊÔ´¡£ÕâЩϵͳ
×ÊÔ
°üÀ¨:Óï¾ä¾ä±ú¡¢Á¬½Ó¾ä±úºÍODBC»·¾³¾ä±ú¡£Íê³ÉÕâ¸ö¹ý³ÌµÄ²½ÖèÈçÏÂ:
*Sµ÷ÓÃSQLFreeStmtº¯ÊýÊÍ·ÅÓï¾ä¾ä±ú¼°ÆäÏà¹ØµÄϵͳ×ÊÔ´¡£ÀýÈç:
rc = SQLFreeStmt(hstmt, SQL_DROP)
*Sµ÷ÓÃSQLDisconnectº¯Êý¹Ø±ÕÁ¬½Ó,ÀýÈç:
rc = SQLDisconnect(hdbc)
*Sµ÷ÓÃSQLFreeConnectº¯ÊýÊÍ·ÅÁ¬½Ó¾ä±ú¼°ÆäÏà¹ØµÄϵͳ×ÊÔ´,ÀýÈç:
rc = SQLFreeConnect(hdbc)
*Sµ÷ÓÃSQLFreeEnvº¯ÊýÊÍ·Å»·¾³¾ä±ú¼°ÆäÏà¹ØµÄϵͳ×ÊÔ´,Í£Ö¹ODBC²Ù×÷,ÀýÈç:
rc = SQLFreeEnv(henv)
´ËÍâ,ÔÚ±àÖƳÌÐòʱ»¹ÓÐÒ»¸öÐèÒªÖص㿼ÂǵÄÎÊÌâ,Õâ¾ÍÊÇ´íÎó´¦Àí¡£ËùÓÐODBC APIº¯Êý
,ÈôÔ
Ö´ÐÐÆڼ䷢Éú´íÎó,¶¼½«·µ»ØÒ»¸ö±ê×¼´íÎó´úÂëSQL_ERROR¡£Ò»°ãÀ´½²,ÔÚÿ´Îµ÷ÓÃ
ODBC
APIº¯ÊýÖ®ºó,¶¼Ó¦¸Ã¼ì²é¸Ãº¯Êý·µ»ØÖµ,È·¶¨¸Ãº¯ÊýÊÇ·ñ³É¹¦µØÖ´ÐÐ,ÔÙ¾ö¶¨ÊÇ·ñ¼ÌÐøºó
Ðø¹ý
¡£¶øÏêϸµÄ´íÎóÐÅÏ¢,¿ÉÒÔµ÷ÓÃSQLErrorº¯Êý»ñµÃ¡£SQLErrorº¯Êý½«·µ»ØÏÂÁÐÐÅÏ¢:
*S±ê×¼µÄODBC´íÎó״̬Âë;
*SODBCÊý¾ÝÔ´ÌṩµÄÄÚ²¿´íÎó±àÂë;
*S´íÎóÐÅÏ¢´®¡£
¼òµ¥Ó¦ÓÃʵÀý
±¾ÊµÀý½«±àÖÆÒ»¸ö¿Í»§»ú¶ËVBÓ¦ÓóÌÐò,ͨ¹ýWindows NT¾ÖÓòÍø²éѯ·þÎñÆ÷¶ËMS SQL Se
rver
6.5Ñù°åÊý¾Ý¿âPUBSÖеÄAUTHORSÊý¾Ý±í,ÔÚÒ»¸öGrid¿Ø¼þÖÐÏÔʾ²éѯ½á¹û¡£Ê×ÏÈ,ʹÓÃWi
ndow
¿ØÖÆÃæ°åÖеÄODBCÇý¶¯¹ÜÀíÆ÷н¨Ò»¸öODBCÊý¾ÝÔ´,¶¨ÒåÊý¾ÝÔ´Ãû³ÆΪODBC_API_DEMO,¶¨
Òåµ
¼Êý¾Ý¿âΪPUBS,ÆäËüÐÅÏ¢Ó¦¸ù¾ÝÓû§µÄ»·¾³ÕýÈ·ÉèÖá£È»ºóÆô¶¯VB,н¨Ò»¸öÏîÄ¿
Project1
ÔÚȱʡ´°ÌåF
orm1ÖмÓÈëÒ»¸öGrid¿Ø¼þGrid1¡¢Á½¸öCommandButton¿Ø¼þcmdQueryºÍcmdClose,ÔÚProje
ct1Ö
²åÈëÒ»¸öÄ£¿éModule1,½«Ç°ÃæÁоٵÄÉùÃ÷Óï¾ä¼ÓÈëÆäÖÐ,³ÌÐò´úÂëÈçÏÂ:
Private Sub Form_Load()
Dim rc As Integer
rc = SQLAllocEnv(henv)
If rc <> 0 Then
MsgBox "ÎÞ·¨³õʼ»¯ODBC"
End
End If
rc = SQLAllocConnect(henv, hdbc)
If rc <> 0 Then
MsgBox "ÎÞ·¨»ñµÃÁ¬½Ó¾ä±ú"
rc = SQLFreeEnv(henv)
End
End If
Dim DSN As String, UID As String, PWD As String
DSN = "ODBC_API_DEMO"
UID = "guest"
PWD = ""
rc = SQLConnect(hdbc, DSN, Len(DSN), UID, Len(UID), PWD, Len(UID))
If rc = SQL_ERROR Then
MsgBox "ÎÞ·¨½¨Á¢ÓëODBCÊý¾ÝÔ´µÄÁ¬½Ó"
End
End If
End Sub
Private Sub cmdQuery_Click()
Dim hstmt As Long
Dim SQLstmt As String
Dim RSCols As Integer, RSRows As Long
Dim rc As Integer, i As Integer, j As Integer
Dim ColVal As String * 1024
Dim ColValLen As Long, ColLabLen As Integer, larg As Long
rc = SQLAllocStmt(hdbc, hstmt)
If rc <> SQL_SUCCESS Then
MsgBox "ÎÞ·¨»ñµÃSQLÓï¾ä¾ä±ú"
Exit Sub
End If
SQLstmt = "SELECT * FROM authors"
rc = SQLExecDirect(hstmt, SQLstmt, Len(SQLstmt))
If rc <> SQL_SUCCESS Then
MsgBox "SQLÓï¾äÖ´ÐÐʧ°Ü"
Exit Sub
End If
rc = SQLNumResultCols(hstmt, RSCols)
If RSCols > 1 Then
Grid1.Cols = RSCols
Grid1.Rows = 10
Grid1.Row = 0
Else
Exit Sub
End If
For i = 1 To RSCols
rc = SQLColAttributesString(hstmt, i, SQL_COLUMN_LABEL, ColVal, 255, Col
LabLen, larg)
Grid1.Col = i - 1
Grid1.Text = Left(ColVal, ColLabLen)
Next i
Do Until SQLFetch(hstmt) = SQL_NO_DATA_FOUND
ColVal = String$(1024, 0)
If Grid1.Row + 1 >= Grid1.Rows Then
Grid1.Rows = Grid1.Rows + 1
End If
Grid1.Row = Grid1.Row + 1
For i = 1 To RSCols
rc = SQLGetData(hstmt, i, SQL_C_CHAR, ColVal, Len(ColVal), ColValLen)
Grid1.Col = i - 1
Grid1.Text = Left$(ColVal, ColValLen)
Next i
Loop
rc = SQLFreeStmt(hstmt, SQL_DROP)
End Sub
Private Sub cmdClose_Click()
Dim rc As Integer
If hdbc <> 0 Then
rc = SQLDisconnect(hdbc)
End If
rc = SQLFreeConnect(hdbc)
If henv <> 0 Then
rc = SQLFreeEnv(henv)
End If
End
End Sub
±¾Àý³ÌÐòÔÚVisual Basic 4.0¡¢Windows 95»·¾³Ïµ÷ÊÔͨ¹ý¡£
--
ÒԿƼÆΪ±¾,ÒÔ²úÒµ±¨¹ú!
³¬Ô½×ÔÎÒ,·ÉÔ¾ÎÞÏÞ!
¡ù À´Ô´:¡¤¹þ¹¤´ó×϶¡Ïã bbs.hit.edu.cn¡¤[FROM: 202.118.235.249]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º2.582ºÁÃë