⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db.inc

📁 一个背单词源码
💻 INC
字号:
include 	odbc32.inc
includelib 	odbc32.lib

ODBCConnect proto :DWORD
ODBCDisconnect proto :DWORD
ODBCExeDirect	proto :dword
GetProgramPath proto
;ODBCAppend		proto :dword,:dword,:dword
;MyGetCurrentDataBase	proto
;MyStatusBarSetText		proto:dword,:dword

.data
SQLStatement db "select * from wordlist",0
;WhereStatement db " where 单词=?",0
strConnect db  "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=",0
DBName db "wordlist.mdb",0
;ConnectCaption db "Complete Connection String",0
Disconnect db "Disconnect successful",0
AllocEnvFail db "Environment handle allocation failed",0
AllocConnFail db "Connection handle allocation failed",0
SetAttrFail db "Cannot set desired ODBC version",0


Heading1 db "序号",0
Heading2 db "单词",0
Heading3 db "解释",0
Heading4 db "例句",0
t_space		db	" ",0

SQLTABLE struct
	szName	db 192 dup(?)
	szType	db 64 dup(?)
SQLTABLE ends


.data?
hEnv dd ?		; environment handle
hConn dd ?	; connection handle
hStmt dd ?
Conn db 256 dup(?)
DBStrLen dd ?
;单词, 解释,音标,词组, 例句,联想
buffer_row_1 db 50 dup(?)
buffer_row_2 db 255 dup(?)
buffer_row_3 db 50 dup(?)
buffer_row_4 db 100 dup(?)
buffer_row_5 db 255 dup(?)
buffer_row_6 db 255 dup(?)
buffer_1_Length dd ?
buffer_2_Length dd ?
buffer_3_Length dd ?
buffer_4_Length dd ?
buffer_5_Length dd ?
buffer_6_Length dd ?
SearchName db 26 dup(?)
ProgPath db 256 dup(?)
ConnectString db 1024 dup(?)
MyBuffer		db 2048 dup(?)

.code
ODBCConnect proc hDlg:DWORD
	invoke SQLAllocHandle, SQL_HANDLE_ENV, SQL_NULL_HANDLE, addr hEnv
	.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
		invoke SQLSetEnvAttr, hEnv,SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3,0
		.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
			invoke SQLAllocHandle, SQL_HANDLE_DBC, hEnv, addr hConn
			.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
				invoke lstrcpy,addr ConnectString,addr strConnect
				invoke lstrcat,addr ConnectString, addr ProgPath
				invoke lstrcat, addr ConnectString,addr DBName
				invoke SQLDriverConnect,hConn, hDlg, addr ConnectString, sizeof ConnectString, addr Conn, sizeof Conn,addr DBStrLen, SQL_DRIVER_COMPLETE
				.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
;					invoke SwitchMenuState,TRUE
;					invoke MessageBox,hDlg, addr Conn,addr ConnectCaption,MB_OK+MB_ICONINFORMATION
				.else
					invoke SQLFreeHandle, SQL_HANDLE_DBC, hConn
					invoke SQLFreeHandle, SQL_HANDLE_ENV, hEnv
					invoke MessageBox, hDlg, SADD("连接数据库出错!"), addr AppName, MB_OK+MB_ICONERROR
				.endif
			.else
				invoke SQLFreeHandle, SQL_HANDLE_ENV, hEnv
				invoke MessageBox, hDlg, addr AllocConnFail, addr AppName, MB_OK+MB_ICONERROR
			.endif
		.else
			invoke SQLFreeHandle, SQL_HANDLE_ENV, hEnv
			invoke MessageBox, hDlg, addr SetAttrFail, addr AppName, MB_OK+MB_ICONERROR
		.endif
	.else
		invoke MessageBox, hDlg, addr AllocEnvFail, addr AppName, MB_OK+MB_ICONERROR	
	.endif
	ret
ODBCConnect endp
GetProgramPath proc
	invoke GetModuleFileName, NULL,addr ProgPath,sizeof ProgPath
	std
	mov edi,offset ProgPath
	add edi,sizeof ProgPath-1
	mov al,"\"
	mov ecx,sizeof ProgPath
	repne scasb
	cld
	mov byte ptr [edi+2],0
	ret
GetProgramPath endp

ODBCDisconnect proc hDlg:DWORD
	invoke SQLDisconnect, hConn
	invoke SQLFreeHandle, SQL_HANDLE_DBC, hConn
	invoke SQLFreeHandle, SQL_HANDLE_ENV, hEnv
;	invoke SwitchMenuState, FALSE
;	invoke ShowWindow,hList, SW_HIDE
;	invoke MessageBox,hDlg,addr Disconnect, addr AppName,MB_OK+MB_ICONINFORMATION
	ret
ODBCDisconnect endp

InsertColumn proc
	LOCAL lvc:LV_COLUMN
	mov lvc.imask,LVCF_TEXT+LVCF_WIDTH
	mov lvc.pszText,offset Heading1
	mov lvc.lx,150
	invoke SendMessage,hList, LVM_INSERTCOLUMN,0,addr lvc
	mov lvc.pszText,offset Heading2
	invoke SendMessage,hList, LVM_INSERTCOLUMN, 1 ,addr lvc	
	mov lvc.pszText,offset Heading3
	invoke SendMessage,hList, LVM_INSERTCOLUMN, 3 ,addr lvc	
	ret		
InsertColumn endp
FillData proc
	
	LOCAL row:DWORD
	LOCAL icount:dword	;count how many data is present;
	
	mov icount, 0
	invoke SQLBindCol, hStmt,1,SQL_C_CHAR, addr buffer_row_1, sizeof buffer_row_1,addr buffer_1_Length
	mov row,0
	invoke SendMessage,hList,LB_RESETCONTENT,0,0
	.while TRUE
;		invoke MessageBox,NULL,SADD("entered fill data"),addr AppName,MB_OK
		mov byte ptr ds:[buffer_row_1],0
		invoke SQLFetch, hStmt
;		单词='%s', 解释='%s',音标='%s',词组='%s', 例句='%s',联想='%s' where id=%s",0
		.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
			inc icount
			invoke SendMessage,hList,LB_ADDSTRING,0,addr buffer_row_1			
		.else			
			.break
		.endif
	.endw
	invoke SQLCloseCursor,hStmt
	invoke SQLFreeHandle,SQL_HANDLE_STMT,hStmt
	ret
FillData endp


;删除指定表中的全部词条
ODBCDelete	proc	tablename:dword
	LOCAL l_len:dword
	;same as zap
	mov [MyBuffer],byte ptr 0
	;invoke MessageBox,NULL,tablename,addr AppName,MB_OK
	invoke lstrcpy,addr MyBuffer,SADD("DELETE FROM ")
	invoke lstrcat,addr MyBuffer,tablename
	invoke lstrlen,addr MyBuffer
	mov l_len,eax
	;invoke MessageBox,NULL,tablename,addr AppName,MB_OK
	;invoke MessageBox,NULL,addr MyBuffer,addr AppName,MB_OK
	invoke SQLAllocHandle, SQL_HANDLE_STMT, hConn, addr hStmt
	.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
		invoke SQLExecDirect, hStmt, addr MyBuffer, l_len 
		.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
;			invoke	MessageBox,NULL,SADD("insert ok"),addr AppName,MB_OK
		.else
			invoke	MessageBox,NULL,SADD("删除数据库出错!"),addr AppName,MB_OK
		.endif
	.endif
	INVOKE SendMessage,hList,LVM_DELETEALLITEMS,0,0
	ret

ODBCDelete endp

	
.data
	updatelistformat	db	"update %s set 单词='%s', 解释='%s',音标='%s',词组='%s', 例句='%s',联想='%s' where 单词='%s'",0
	insertlistformat	db	"insert into %s (单词, 解释,音标,词组, 例句,联想) values ('%s', '%s', '%s', '%s', '%s', '%s')",0
.code
ODBCUpdate	proc uses esi tablename:dword,lpitem:dword,oldWord:dword
	LOCAL lbuf[1024]:byte		
	
	
	assume esi:ptr Item
	mov esi,lpitem
	
	;invoke MessageBox,NULL,addr [esi].m_word,addr AppName,MB_OK	
	
	invoke lstrlen,addr [esi].m_word
	.if eax==0
		invoke lstrcpy,addr [esi].m_word,addr t_space
	.endif
        invoke lstrlen,addr [esi].m_expl
	.if eax==0
		invoke lstrcpy,addr [esi].m_expl,addr t_space
	.endif
        invoke lstrlen,addr [esi].m_phon
	.if eax==0
		invoke lstrcpy,addr [esi].m_phon,addr t_space
	.endif
        invoke lstrlen,addr [esi].m_phrase
	.if eax==0
		invoke lstrcpy,addr [esi].m_phrase,addr t_space
	.endif
        invoke lstrlen,addr [esi].m_eg
	.if eax==0
		invoke lstrcpy,addr [esi].m_eg,addr t_space
	.endif
        invoke lstrlen,addr [esi].m_legd
	.if eax==0
		invoke lstrcpy,addr [esi].m_legd,addr t_space
	.endif
	;invoke MessageBox,NULL,SADD("before write"),addr AppName,MB_OK
	mov [lbuf],byte ptr 0
	
	invoke wsprintf,addr lbuf,addr updatelistformat,tablename,addr [esi].m_word,\
					addr [esi].m_expl,addr [esi].m_phon, addr [esi].m_phrase,\
					addr [esi].m_eg,addr [esi].m_legd,oldWord
	invoke ODBCExeDirect,addr lbuf
	assume esi:nothing
	ret

ODBCUpdate endp
ODBCInserToDB	proc dbname:dword,danci:dword,jieshi:dword,yinbiao:dword\
					 ,cizu:dword,liju:dword,lianxiang:dword
	LOCAL lbuf[1024]:byte
	invoke lstrlen,danci
	.if eax==0
		invoke lstrcpy,danci,addr t_space
	.endif
        invoke lstrlen,jieshi
	.if eax==0
		invoke lstrcpy,jieshi,addr t_space
	.endif
        invoke lstrlen,yinbiao
	.if eax==0
		invoke lstrcpy,yinbiao,addr t_space
	.endif
        invoke lstrlen,cizu
	.if eax==0
		invoke lstrcpy,cizu,addr t_space
	.endif
        invoke lstrlen,liju
	.if eax==0
		invoke lstrcpy,liju,addr t_space
	.endif
        invoke lstrlen,lianxiang
	.if eax==0
		invoke lstrcpy,lianxiang,addr t_space
	.endif	
	invoke wsprintf,addr lbuf,addr insertlistformat,dbname,danci,jieshi,yinbiao,cizu,liju,lianxiang		
	lea eax,lbuf
	ret

ODBCInserToDB endp


ODBCExeDirect	proc htxt:dword
	LOCAL l_len:dword	
	invoke lstrlen,htxt
	mov l_len,eax
	;invoke MessageBox,NULL,tablename,addr AppName,MB_OK
	;invoke MessageBox,NULL,addr MyBuffer,addr AppName,MB_OK
	invoke SQLAllocHandle, SQL_HANDLE_STMT, hConn, addr hStmt
	.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
		invoke SQLExecDirect, hStmt,htxt, l_len 
		.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO			
		.else
			invoke	MessageBox,NULL,htxt,SADD("直接执行出错!!!!"),MB_OK			
		.endif
	.endif
	
	ret

ODBCExeDirect endp


MainSearchAll proc hWin:dword	
	
	LOCAL buf[2048]:byte
	
	
;	invoke GetDlgItemText,hWin,IDC_EDT_SEARCHALL,addr buf,2047
	.if !eax
		ret
	.endif	
	;invoke MessageBox,NULL,addr buf,addr AppName,MB_OK
	invoke ODBCExeDirect,addr buf
	.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
		invoke SendMessage,hList,LVM_DELETEALLITEMS,0,0
		invoke FillData
	.endif
	ret

MainSearchAll endp

ODBCGetTables proc uses edi
	LOCAL	local_hStmt:DWORD
	LOCAL	len1:DWORD
	LOCAL	len2:DWORD

	invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,64*1024
	invoke GlobalLock,eax
	push	eax
	mov		edi,eax
	invoke SQLAllocHandle,SQL_HANDLE_STMT,hConn,addr local_hStmt
	.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
		invoke SQLTables,local_hStmt,NULL,0,NULL,0,NULL,0,NULL,0
		.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
		  @@:
			invoke SQLBindCol,local_hStmt,3,SQL_C_CHAR,addr [edi].SQLTABLE.szName,sizeof SQLTABLE.szName,addr len1
			invoke SQLBindCol,local_hStmt,4,SQL_C_CHAR,addr [edi].SQLTABLE.szType,sizeof SQLTABLE.szType,addr len2
			invoke SQLFetch,local_hStmt
			.if ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO
				lea		edi,[edi+sizeof SQLTABLE]
				jmp		@b
			.endif
		.else
			MyDebug 'Unable to get tables.'
		.endif
		invoke SQLFreeHandle,SQL_HANDLE_STMT,local_hStmt
	.else
		MyDebug 'Unable to allocate statement handle.'
	.endif
	pop		eax
	ret

ODBCGetTables endp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -