日期:2013-10-01  浏览次数:20458 次

一个例子
这一章,我们要把我们已学的知识集合起来。具体来讲,我们来写一个使用ODBC APIs的程序.为简单起见,这个程序中我使用Microsoft的Access数据库(Microsoft Access 97) .

下载例子源程序.

留意:如果你使用的windows.inc 是1.18及其以下版本,在开始编译之前要修正其中的一个小bug.在windows.inc中查找 "SQL_NULL_HANDLE",将得到下面这行:

SQL_NULL_HANDLE equ 0L

将0后面的"L"删除,象这样:

SQL_NULL_HANDLE equ 0

这个程序是一个基于对话框的程序,有一个简单的菜单.当用户选择"connect"时,它将试图连接test.mdb数据库,如果连接成功,将显示由ODBC驱动程序前往的完整连接字符串.接下来,用户可选择"View All Records"命令,程序会使用listview control来显示数据库中的所无数据.用户还可以选择"Query"命令来查询特定的记录.例子程序将会显示一个小对话框提示用户输入想找的人名.当用户按下OK钮或回车键,程序将执行一个查询来查找符合条件的记录.当用户完成对数据库的操作时,可以选择"disconnect"命令与数据库断开连接.

如今看一下源程序:

.386.model flat,stdcallinclude \masm32\include\windows.incinclude \masm32\include\kernel32.incinclude \masm32\include\odbc32.incinclude \masm32\include\comctl32.incinclude \masm32\include\user32.incincludelib \masm32\lib\odbc32.libincludelib \masm32\lib\comctl32.libincludelib \masm32\lib\kernel32.libincludelib \masm32\lib\user32.libIDD_MAINDLG equ 101IDR_MAINMENU equ 102IDC_DATALIST equ 1000IDM_CONNECT equ 40001IDM_DISCONNECT equ 40002IDM_QUERY equ 40003IDC_NAME equ 1000IDC_OK equ 1001IDC_CANCEL equ 1002IDM_CUSTOMQUERY equ 40004IDD_QUERYDLG equ 102DlgProc proto hDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORDQueryProc proto hDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORDSwitchMenuState proto :DWORDODBCConnect proto :DWORDODBCDisconnect proto :DWORDRunQuery proto :DWORD.data?hInstance dd ?hEnv dd ?hConn dd ?hStmt dd ?Conn db 256 dup(?)StrLen dd ?hMenu dd ? ; 主菜单句柄hList dd ? ; listview control句柄TheName db 26 dup(?)TheSurname db 26 dup(?)TelNo db 21 dup(?)NameLength dd ?SurnameLength dd ?TelNoLength dd ?SearchName db 26 dup(?)ProgPath db 256 dup(?)ConnectString db 1024 dup(?).dataSQLStatement db "select * from main",0WhereStatement db " where name=?",0strConnect db "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=",0DBName db "test.mdb",0ConnectCaption db "Complete Connection String",0Disconnect db "Disconnect successful",0AppName db "ODBC Test",0AllocEnvFail db "Environment handle allocation failed",0AllocConnFail db "Connection handle allocation failed",0SetAttrFail db "Cannot set desired ODBC version",0NoData db "You must type the name in the edit box",0ExecuteFail db "Execution of SQL statement failed",0ConnFail db "Connection attempt failed",0AllocStmtFail db "Statement handle allocation failed",0Heading1 db "Name",0Heading2 db "Surname",0Heading3 db "Telephone No.",0.codestart:invoke GetModuleHandle, NULLmov hInstance,eaxcall GetProgramPathinvoke DialogBoxParam, hInstance, IDD_MAINDLG,0,addr DlgProc,0invoke ExitProcess,eaxinvoke InitCommonControlsDlgProc proc hDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD.if uMsg==WM_INITDIALOGinvoke GetMenu, hDlgmov hMenu,eaxinvoke GetDlgItem, hDlg, IDC_DATALISTmov hList,eaxcall InsertColumn.elseif uMsg==WM_CLOSEinvoke GetMenuState, hMenu, IDM_CONNECT,MF_BYCOMMAND.if eax==MF_GRAYEDinvoke ODBCDisconnect, hDlg.endifinvoke EndDialog,hDlg, 0.elseif uMsg==WM_COMMAND.if lParam==0mov eax,wParam.if ax==IDM_CONNECTinvoke ODBCConnect,hDlg.elseif ax==IDM_DISCONNECTinvoke ODBCDisconnect,hDlg.elseif ax==IDM_QUERYinvoke RunQuery,hDlg.elseif ax==IDM_CUSTOMQUERYinvoke DialogBoxParam, hInstance, IDD_QUERYDLG,hDlg, addr QueryProc, 0.endif.endif.elsemov eax,FALSEret.endifmov eax,TRUEretDlgProc endp GetProgramPath procinvoke GetModuleFileName, NULL,addr ProgPath,sizeof ProgPathstdmov edi,offset ProgPathadd edi,sizeof ProgPath-1mov al,"\"mov ecx,sizeof ProgPathrepne scasbcldmov byte ptr [edi+2],0retGetProgramPath endpSwitchMenuState proc Flag:DWORD.if Flag==TRUE invoke EnableMenuItem, hMenu, IDM_CONNECT, MF_GRAYEDinvoke EnableMenuItem, hMenu, IDM_DISCONNECT, MF_ENABLEDinvoke EnableMenuItem, hMenu, IDM_QUERY, MF_ENABLEDinvoke EnableMenuItem, hMenu, IDM_CUSTOMQUERY, MF_ENABLED.elseinvoke EnableMenuItem, hMenu, IDM_CONNECT, MF_ENABLEDinvoke EnableMenuItem, hMenu, IDM_DISCONNECT, MF_GRAYEDinvoke EnableMenuItem, hMenu, IDM_QUERY, MF_GRAYEDinvoke EnableMenuItem, hMen