直接通过ODBC API访问SQL数据库

原创 2002年01月15日 09:21:00

*********************************
ODBC - Open DataBase Connectivity
*********************************

Basic Steps

Connecting to the SQL Server DataBase for retrieving information from tables


*************************************************************
The steps 1 - 3 are for connecting to the SQL Server Database
*************************************************************


1. Allocate ODBC Environment Handle

If SQLAllocEnv(glEnv) <> 0 Then
  MsgBox "Unable to initialize ODBC API drivers!"
  End
End If
______________________________________________________________

2. Allocate ODBC Database Handle

Dim iStatus As Integer

If SQLAllocConnect(glEnv, glDbc) <> 0 Then
  MsgBox "Could not allocate memory for connection Handle!"
  ODBCInit = False

  ' Free the Environment
  iStatus = SQLFreeEnv(lEnv)

  If iStatus = SQL_ERROR Then
    MsgBox "Error Freeing Environment From ODBC Drivers"
  End If

  ' Quit the Application
  End
End If
______________________________________________________________

3. Connect using the sConnect string - SQLDriverConnect

Dim sResult As String
Dim iSize As Integer
Dim sConnect As String

sConnect = "DSN=" & gsDSN & ";UID=" & gsLoginID & ";PWD=" & gsPassword & ";APP=" & gsAppCode & ";DATABASE=" & gsDatabase

If SQLDriverConnect(glDbc, Screen.ActiveForm.hWnd, sConnect, Len(sConnect), sResult, Len(sResult), iSize, 0) <= 0 Then
  MsgBox "Could not establish connection to ODBC driver!"
End If
______________________________________________________________


***************************************************
The steps 4 - 8 are for retrieving data from tables
***************************************************


4. Allocate ODBC Statement Handle

If SQLAllocStmt(glDbc, glStmt) <> 0 Then

   MsgBox "Could not allocate memory for a statement handle!"

End If
______________________________________________________________

5. Execute ODBC Statement - SQLExecDirect

Dim lRet As Long, lErrNo As Long
Dim iLen As Integer
Dim sSQLState As String * MAX_DATA_BUFFER
Dim sErrorMsg As String * MAX_DATA_BUFFER
Dim sMsg As String

sSQL = "SELECT name, location FROM authors"

If SQLExecDirect(glStmt, sSQL, Len(sSQL)) <> SQL_SUCCESS Then
  ' Also Check for ODBC Error message - SQLError
  lRet = SQLError(glEnv, gldbc, glStmt, sSQLState, lErrNo, sErrorMsg, MAX_DATA_BUFFER, iLen)
  sMsg = "Error Executing SQL Statement" & Chr$(13) & Chr$(10)
  sMsg = sMsg & "ODBC State = " & Trim$(Left$(sSQLState, InStr(sSQLState, Chr$(0)) - 1)) & Chr$(13) & Chr$(10)
  sMsg = sMsg & "ODBC Error Message = " & Left$(sErrorMsg, iLen)
  MsgBox sMsg, vbInformation, "Execute Query"
End If
______________________________________________________________

6. Fetch one row of results from executed ODBC Statement - SQLFetch

Code in Step 7.
______________________________________________________________

7. Get the Data in each field of the Fetched row - SQLGetData

Dim bPerform As Integer, iStatus As Integer
Dim sData As String * MAX_DATA_BUFFER
Dim lOutLen As Long

bPerform = SQLFetch(glStmt)

Do While bPerform
  bPerform = SQLFetch(lStmt)     ' Get the next row of data
  If bPerform = SQL_SUCCESS Then    ‘ If rows of data available  
    bPerform = True

    ' Get Author Name - iColumn = 1 for first field i.e. name in sSQL
    iStatus = SQLGetData(glStmt, iColumn, 1, sData, MAX_DATA_BUFFER, lOutLen)

    ' lOutlen = length of the valid data in sData
    ' Data value will be = Left$(sData, lOutlen), lOutlen = -1 if no data or Null data

    ' Get Location - iColumn = 2 for second field i.e. location in sSQL
    iStatus = SQLGetData(glStmt, iColumn, 1, sData, MAX_DATA_BUFFER, lOutLen)

     ' Add the Field Data to Correponding Data Display Controls for this row
  Else
    bPerform = False  ' No more rows available
  End If
Loop

'Release the ODBC Statement Handle
bPerform = SQLFreeStmt(glStmt, SQL_DROP)
______________________________________________________________

8. Release the ODBC Statement Handle - SQLFreeSTmt

Code in Step 7.
______________________________________________________________

*******************************************************************
The steps 9 - 11 are for Disconnecting from the SQL Server DataBase
*******************************************************************


9. Disconnect from ODBC Database - SQLDisconnect

iStatus = SQLDisconnect(glDbc)
______________________________________________________________

10. Release the ODBC Database Handle - SQLFreeConnect

iStatus = SQLFreeConnect(glDbc)
______________________________________________________________

11. Release the ODBC Environment Handle - SQLFreeEnv

iStatus = SQLFreeEnv(glEnv)
______________________________________________________________

***********************************************************************
The following entries are required in the ODBCAPI module
***********************************************************************

' ODBC Variables and Constants
 
Global glEnv As Long
Global glDbc As Long
Global sSQL As String
 
Global Const MAX_DATA_BUFFER = 255
Global Const SQL_SUCCESS = 0
Global Const SQL_SUCCESS_WITH_INFO = 1
Global Const SQL_ERROR = -1
Global Const SQL_NO_DATA_FOUND = 100
Global Const SQL_CLOSE = 0
Global Const SQL_DROP = 1
Global Const SQL_CHAR = 1
Global Const SQL_NUMERIC = 2
Global Const SQL_DECIMAL = 3
Global Const SQL_INTEGER = 4
Global Const SQL_SMALLINT = 5
Global Const SQL_FLOAT = 6
Global Const SQL_REAL = 7
Global Const SQL_DOUBLE = 8
Global Const SQL_VARCHAR = 12
Global Const SQL_DATA_SOURCE_NAME = 6
Global Const SQL_USER_NAME = 8


'ODBC Declarations
'The hWnd is a Long in Windows 95 & Windows NT

#If Win32 Then
  Declare Function SQLAllocEnv Lib "odbc32.dll" (env As Long) As Integer
  Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal env As Long) As Integer
  Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal env As Long, ldbc As Long) As Integer
  Declare Function SQLConnect Lib "odbc32.dll" (ByVal ldbc As Long, ByVal Server As String,ByVal serverlen As Integer, ByVal uid As String, ByVal   uidlen As Integer, ByVal pwd As String, ByVal pwdlen As Integer) As Integer

  Declare Function SQLDriverConnect Lib "odbc32.dll" (ByVal ldbc As Long, ByVal hWnd As Long, ByVal szCSIn As String, ByVal cbCSIn As   Integer,ByVal szCSOut As String, ByVal cbCSMax As Integer, cbCSOut As Integer, ByVal f As Integer) As Integer

  Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal ldbc As Long) As Integer
  Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal ldbc As Long) As Integer
  Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal ldbc As Long, lStmt As Long) As Integer
  Declare Function SQLFreeStmt Lib "odbc32.dll" (ByVal lStmt As Long, ByVal EndOption As Integer) As Integer
  Declare Function SQLTables Lib "odbc32.dll" (ByVal lStmt As Long, ByVal q As Long, ByVal cbq As Integer, ByVal o As Long, ByVal cbo As Integer,   ByVal t As Long, ByVal cbt As Integer, ByVal tt As Long, ByVal cbtt As Integer) As Integer

  Declare Function SQLExecDirect Lib "odbc32.dll" (ByVal lStmt As Long, ByVal sqlString As String, ByVal sqlstrlen As Long) As Integer

  Declare Function SQLNumResultCols Lib "odbc32.dll" (ByVal lStmt As Long, NumCols As Integer) As Integer
  Declare Function SQLDescribeCol Lib "odbc32.dll" (ByVal lStmt As Long, ByVal colnum As Integer, ByVal colname As String, ByVal Buflen As   Integer, colnamelen As Integer, dtype As Integer, dl As Long, ds As Integer, n As Integer) As Integer

  Declare Function SQLFetch Lib "odbc32.dll" (ByVal lStmt As Long) As Integer
  Declare Function SQLGetData Lib "odbc32.dll" (ByVal lStmt As Long, ByVal col As Integer, ByVal wConvType As Integer, ByVal lpbBuf As String,   ByVal dwbuflen As Long, lpcbout As Long) As Integer

  Declare Function SQLGetInfo Lib "odbc32.dll" (ByVal ldbc As Long, ByVal hWnd As Long, ByVal szInfo As String, ByVal cbInfoMax As Integer,   cbInfoOut As Integer) As Integer

  Declare Function SQLError Lib "odbc32.dll" (ByVal env As Long, ByVal ldbc As Long, ByVal lStmt As Long, ByVal SQLState As String, NativeError As   Long, ByVal Buffer As String, ByVal Buflen As Integer, Outlen As Integer) As Integer

#Else

  Declare Function SQLAllocEnv Lib "odbc.dll" (env As Long) As Integer
  Declare Function SQLFreeEnv Lib "odbc.dll" (ByVal env As Long) As Integer
  Declare Function SQLAllocConnect Lib "odbc.dll" (ByVal env As Long, ldbc As Long) As Integer
  Declare Function SQLConnect Lib "odbc.dll" (ByVal ldbc As Long, ByVal Server As String, ByVal serverlen As Integer, ByVal uid As String, ByVal   uidlen As Integer, ByVal pwd As String, ByVal pwdlen As Integer) As Integer

  Declare Function SQLDriverConnect Lib "odbc.dll" (ByVal ldbc As Long, ByVal hWnd As Integer, ByVal szCSIn As String, ByVal cbCSIn As Integer,   ByVal szCSOut As String, ByVal cbCSMax As Integer, cbCSOut As Integer, ByVal f As Integer) As Integer

  Declare Function SQLFreeConnect Lib "odbc.dll" (ByVal ldbc As Long) As Integer
  Declare Function SQLDisconnect Lib "odbc.dll" (ByVal ldbc As Long) As Integer
  Declare Function SQLAllocStmt Lib "odbc.dll" (ByVal ldbc As Long, lStmt As Long) As Integer
  Declare Function SQLFreeStmt Lib "odbc.dll" (ByVal lStmt As Long, ByVal EndOption As Integer) As Integer
  Declare Function SQLTables Lib "odbc.dll" (ByVal lStmt As Long, ByVal q As Long, ByVal cbq As Integer, ByVal o As Long, ByVal cbo As Integer,   ByVal t As Long, ByVal cbt As Integer, ByVal tt As Long, ByVal cbtt As Integer) As Integer

  Declare Function SQLExecDirect Lib "odbc.dll" (ByVal lStmt As Long, ByVal sqlString As String, ByVal sqlstrlen As Long) As Integer

  Declare Function SQLNumResultCols Lib "odbc.dll" (ByVal lStmt As Long, NumCols As Integer) As Integer
  Declare Function SQLDescribeCol Lib "odbc.dll" (ByVal lStmt As Long, ByVal colnum As Integer, ByVal colname As String, ByVal Buflen As Integer,   colnamelen As Integer, dtype As Integer, dl As Long, ds As Integer, n As Integer) As Integer

  Declare Function SQLFetch Lib "odbc.dll" (ByVal lStmt As Long) As Integer
  Declare Function SQLGetData Lib "odbc.dll" (ByVal lStmt As Long, ByVal col As Integer, ByVal wConvType As Integer, ByVal lpbBuf As String, ByVal   dwbuflen As Long, lpcbout As Long) As Integer

  Declare Function SQLGetInfo Lib "odbc.dll" (ByVal ldbc As Long, ByVal hWnd As Integer, ByVal szInfo As String, ByVal cbInfoMax As   Integer,cbInfoOut As Integer) As Integer

  Declare Function SQLError Lib "odbc.dll" (ByVal env As Long, ByVal ldbc As Long, ByVal lStmt As Long, ByVal SQLState As String, NativeError As   Long, ByVal Buffer As String, ByVal Buflen As Integer, Outlen As Integer) As Integer

#End If

VS2013 MFC ODBC连接SQL SERVER数据库编程(三)

VS2013 MFC ODBC连接SQL SERVER数据库编程(三) ODBC Crecordset的操作 数据库sql server的增删查改等操作...
  • my_acm
  • my_acm
  • 2014年12月18日 17:43
  • 4297

ODBC连接数据库总结

注:本文内容主要摘自《数据库教程》电子工业出版社,施伯乐等编著。sinble总结   句柄是应用程序和数据源连接的工具。根据应用程序的请求,驱动程序管理器就各驱动程序为存储有关OD...
  • ghevinn
  • ghevinn
  • 2015年02月11日 17:33
  • 2569

wincc 数据库的连接方法

• WinCC 中如何与本地数据库(如SQL SERVER、ORACLE)进行数据交换? 以下的测试将说明如连接远程的数据库: Topic One: ODBC Testing environme...
  • ddjj_1980
  • ddjj_1980
  • 2013年03月22日 14:13
  • 2615

VC++6.0环境下用ODBC连接SQL Server数据库的简单例子

以前一直在用ADO的方式来连接数据库,但是最近的新项目中遇到了一个问题,客户非得用ORACLE的数据库来做,而我们的系统连接ORACLE数据库用的是ODBC,于是我也趁机学习了一下关于ODBC来连接数...
  • polo19
  • polo19
  • 2015年01月27日 15:20
  • 3007

ODBC API开发教程1

第 1 章        介绍... 2 第 2 章        ODBC API访问数据库... 2 2.1       ODBC简要介绍... 2 2.1.1        在...
  • damenhanter
  • damenhanter
  • 2014年11月27日 01:30
  • 1220

java访问SQLServer2008数据库方式一:jdbc-odbc桥连接

import java.sql.*; public class Test2 { public static void main(String[] args) { //定义需要的对象 Co...
  • Geek_ymv
  • Geek_ymv
  • 2013年09月07日 14:55
  • 3863

VS2013 MFC ODBC连接SQL SERVER数据库编程(二)

VS2013 MFC ODBC连接SQL SERVER数据库编程(二) 转载请注明:http://blog.csdn.net/my_acm/article/category/2616577 在上...
  • my_acm
  • my_acm
  • 2014年12月13日 11:31
  • 6232

pyodbc访问数据库(python ODBC访问数据库)

python通过ODBC访问数据库。使用pyodbc,快速达成目标
  • supperman_009
  • supperman_009
  • 2016年07月08日 17:34
  • 7274

Mysql和SqlServer互相转换-数据源ODBC方法

环境: Windows XP sp2 MS SQL Server 2OOO sp1 MySql 5.0.41 1:MSSQLServer数据库导入到MySql数据库 步骤: 1.安装mysql数据库的...
  • xmlife
  • xmlife
  • 2015年01月17日 17:51
  • 1872

ODBC与ADO的区别

ODBC(Open Database Connection,开放式数据互连)是访问数据库的一个统一接口标准,它允许开发人员使用ODBC API(应用程序接口)来访问多种不同的数据源,并执行数据操作。 ...
  • foreverhuylee
  • foreverhuylee
  • 2014年05月16日 11:21
  • 884
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:直接通过ODBC API访问SQL数据库
举报原因:
原因补充:

(最多只允许输入30个字)