Code
.386
.model flat, stdcall
option casemap : none
include windows.inc
include odbc32.inc
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
includelib odbc32.lib
OnError macro
.if ax != SQL_SUCCESS && ax != SQL_SUCCESS_WITH_INFO
jmp @F
.endif
endm
.data?
hEnv dd ? ;连接句柄
hConn dd ? ;环境句柄
.code
DllEntry proc _hInstance, _dwReason, _dwReserved
mov eax, TRUE
ret
DllEntry endp
;设置环境
;返回值:执行成功1,否则返回0
_SetEnv proc
pushad
invoke SQLAllocHandle, SQL_HANDLE_ENV, SQL_NULL_HANDLE, addr hEnv
OnError
invoke SQLSetEnvAttr, hEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3,0
OnError
invoke SQLAllocHandle, SQL_HANDLE_DBC, hEnv, addr hConn
OnError
invoke SQLSetConnectAttr,hConn,SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF,0
popad
mov eax, 1
ret
@@:
popad
mov eax, 0
ret
_SetEnv endp
;通过连接字符串连接到数据库
;hWnd:窗体句柄
;lpConnString:连接字符串
;dwStrLen:连接字符串的长度
;dwDriverCompletion:连接错误的提示方式
;返回值:执行成功返1,否则返回0
_Connect proc hWnd : dword, lpConnString : dword, dwStrLen : dword, dwDriverCompletion : dword
local @szFullString[100] : byte
local @dwLen : dword
pushad
invoke _SetEnv
;如果环境设置失败则返回0
cmp eax, 0
je @F
;链接到数据库
invoke SQLDriverConnect, hConn, hWnd, lpConnString, dwStrLen,\
addr @szFullString, sizeof @szFullString, addr @dwLen, dwDriverCompletion
OnError
popad
mov eax, 1
ret
@@:
popad
mov eax, 0
ret
_Connect endp
;函数说明:通过DSN链接数据库
;lpDsn:DSN名称
;dwLpDsnLen:DSN名称长度
;返回值:0表示连接成功,-1表示连接失败
_ConnectByDsn proc lpDsn : dword, dwLpDsnLen : dword
invoke _SetEnv
;链接到数据库
invoke SQLConnect, hConn, addr lpDsn, -1, NULL, NULL, NULL, NULL
OnError
mov eax, 0
ret
@@:
mov eax, -1
ret
_ConnectByDsn endp
;释放资源
_Dispose proc
.if hConn
invoke SQLEndTran,SQL_HANDLE_DBC,hConn,SQL_COMMIT
invoke SQLDisconnect,hConn
invoke SQLFreeHandle,SQL_HANDLE_DBC,hConn
.endif
.if hEnv
invoke SQLFreeHandle,SQL_HANDLE_ENV,hEnv
.endif
ret
_Dispose endp
;函数说明:执行Sql语句
;lpSql:Sql语句
;dwLpSqlLen:Sql语句长度
;返回值:执行成功返回影响行数,否则返回-1
_ExcuteSql proc lpSql : dword
local @hStmt : dword
invoke SQLAllocHandle, SQL_HANDLE_STMT, hConn, addr @hStmt
OnError
invoke SQLSetStmtAttr, @hStmt, SQL_ATTR_CURSOR_TYPE, SQL_CURSOR_STATIC, 0
OnError
;执行SQL
invoke lstrlen, lpSql
invoke SQLExecDirect, @hStmt, lpSql, eax
OnError
invoke SQLCloseCursor, @hStmt
invoke SQLFreeHandle, SQL_HANDLE_STMT, @hStmt
invoke _Dispose
mov eax, 0
ret
@@:
mov eax, -1
ret
_ExcuteSql endp
end DllEntry
掉了一句话调试了N半天,晕死!