📄 _database.asm
字号:
.data?
hConn dd ?
hEnv dd ?
szFullString db 1024 dup (?) ;连接后返回的全字符串
.const
szConnString db "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=.\mdb.mdb;UID=admin;PWD=qbasic",0
szCheckPass db "select * from USERS where username='%s' and password='%s'",0
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 断开到数据库的连接
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_DisConnect proc
.if hConn
invoke SQLDisconnect,hConn
invoke SQLFreeHandle,SQL_HANDLE_DBC,hConn
.endif
.if hEnv
invoke SQLFreeHandle,SQL_HANDLE_ENV,hEnv
.endif
xor eax,eax
mov hConn,eax
mov hEnv,eax
ret
_DisConnect endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 连接到数据库
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_Connect proc
local @dwTemp
;********************************************************************
; 申请环境句柄和连接句柄
;********************************************************************
invoke SQLAllocHandle,SQL_HANDLE_ENV,SQL_NULL_HANDLE,addr hEnv
.if ax != SQL_SUCCESS && ax != SQL_SUCCESS_WITH_INFO
jmp _Error
.endif
invoke SQLSetEnvAttr,hEnv,SQL_ATTR_ODBC_VERSION,SQL_OV_ODBC3,0
.if ax != SQL_SUCCESS && ax != SQL_SUCCESS_WITH_INFO
jmp _Error
.endif
invoke SQLAllocHandle,SQL_HANDLE_DBC,hEnv,addr hConn
.if ax != SQL_SUCCESS && ax != SQL_SUCCESS_WITH_INFO
jmp _Error
.endif
;********************************************************************
; 连接到数据库
;********************************************************************
invoke lstrlen,addr szConnString
mov ecx,eax
invoke SQLDriverConnect,hConn,NULL,addr szConnString,ecx,\
addr szFullString,sizeof szFullString,addr @dwTemp,SQL_DRIVER_COMPLETE
.if ax == SQL_SUCCESS || ax == SQL_SUCCESS_WITH_INFO
xor eax,eax
inc eax
.else
_Error:
invoke _DisConnect
xor eax,eax
.endif
ret
_Connect endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 执行SQL语句
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_Execute proc uses esi _lphStmt,_lpszSQL
mov esi,_lphStmt
invoke SQLAllocHandle,SQL_HANDLE_STMT,hConn,esi
.if ax != SQL_SUCCESS && ax != SQL_SUCCESS_WITH_INFO
jmp _Error
.endif
invoke SQLSetStmtAttr,[esi],SQL_ATTR_CURSOR_TYPE,SQL_CURSOR_STATIC,0
;********************************************************************
; 执行 SQL 语句
;********************************************************************
invoke lstrlen,_lpszSQL
invoke SQLExecDirect,[esi],_lpszSQL,eax
.if ax != SQL_SUCCESS && ax != SQL_SUCCESS_WITH_INFO && ax != SQL_NO_DATA;执行失败
invoke SQLFreeHandle,SQL_HANDLE_STMT,[esi]
_Error:
xor eax,eax
mov [esi],eax ;这句干嘛呢?
inc eax
ret
.endif
xor eax,eax
ret
_Execute endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_CheckPassword proc _lpszUsername,_lpszPassword
local @szBuffer[1024]:byte,@hStmt,@stRs:ODBC_RS
local @dwReturn
local @dwTemp,@dwErrCode
local @szSQLState[8]:byte,@szMsg[SQL_MAX_MESSAGE_LENGTH]:byte
local @dwRecordCols,@dwResultRows
local @szName[128]:byte,@dwNameSize,@dwType,@dwSize,@dwSize1,@dwNullable
; local @stRs:ODBC_RS,@hStmt
xor eax,eax
mov @dwReturn,eax
invoke wsprintf,addr @szBuffer,addr szCheckPass,_lpszUsername,_lpszPassword
invoke _Execute,addr @hStmt,addr @szBuffer
.if eax ;eax是1表明执行后错误或无数据
jmp _Err
.endif
invoke _RsOpen,addr @stRs,@hStmt
invoke _RsMoveNext,addr @stRs
.if eax
_Err:
inc @dwReturn
.endif
invoke _RsClose,addr @stRs
invoke SQLCloseCursor,@hStmt
invoke SQLFreeHandle,SQL_HANDLE_STMT,@hStmt
mov eax,@dwReturn
ret
_CheckPassword endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -