一个例子

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
一个例子
这一章,我们要把我们已学的知识集合起来。具体来讲,我们来写一个使用ODBCAPIs的程序.为简单起见,这个程序中我使用Microsoft的(MicrosoftAccess97).

下载例子源程序.

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

SQL_NULL_HANDLEequ0L

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

SQL_NULL_HANDLEequ0

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

现在看一下源程序:

.386.modelflat,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_MAINDLGequ101IDR_MAINMENUequ102IDC_DATALISTequ1000IDM_CONNECTequ40001IDM_DISCONNECTequ40002IDM_QUERYequ40003IDC_NAMEequ1000IDC_OKequ1001IDC_CANCELequ1002IDM_CUSTOMQUERYequ40004IDD_QUERYDLGequ102DlgProcprotohDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORDQueryProcprotohDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORDSwitchMenuStateproto:DWORDODBCConnectproto:DWORDODBCDisconnectproto:DWORDRunQueryproto:DWORD.data?hInstancedd?hEnvdd?hConndd?hStmtdd?Conndb256dup(?)StrLendd?hMenudd?;主菜单句柄hListdd?;listviewcontrol句柄TheNamedb26dup(?)TheSurnamedb26dup(?)TelNodb21dup(?)NameLengthdd?SurnameLengthdd?TelNoLengthdd?SearchNamedb26dup(?)ProgPathdb256dup(?)ConnectStringdb1024dup(?).dataSQLStatementdb"select*frommain",0WhereStatementdb"wherename=?",0strConnectdb"DRIVER={MicrosoftAccessDriver(*.mdb)};DBQ=",0DBNamedb"test.mdb",0ConnectCaptiondb"CompleteConnectionString",0Disconnectdb"Disconnectsuccessful",0AppNamedb"ODBCTest",0AllocEnvFaildb"Environmenthandleallocationfailed",0AllocConnFaildb"Connectionhandleallocationfailed",0SetAttrFaildb"CannotsetdesiredODBCversion",0NoDatadb"Youmusttypethenameintheeditbox",0ExecuteFaildb"ExecutionofSQLstatementfailed",0ConnFaildb"Connectionattemptfailed",0AllocStmtFaildb"Statementhandleallocationfailed",0Heading1db"Name",0Heading2db"Surname",0Heading3db"TelephoneNo.",0.codestart:invokeGetModuleHandle,NULLmovhInstance,eaxcallGetProgramPathinvokeDialogBoxParam,hInstance,IDD_MAINDLG,0,addrDlgProc,0invokeExitProcess,eaxinvokeInitCommonControlsDlgProcprochDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD.ifuMsg==WM_INITDIALOGinvokeGetMenu,hDlgmovhMenu,eaxinvokeGetDlgItem,hDlg,IDC_DATALISTmovhList,eaxcallInsertColumn.elseifuMsg==WM_CLOSEinvokeGetMenuState,hMenu,IDM_CONNECT,MF_BYCOMMAND.ifeax==MF_GRAYEDinvokeODBCDisconnect,hDlg.endifinvokeEndDialog,hDlg,0.elseifuMsg==WM_COMMAND.iflParam==0moveax,wParam.ifax==IDM_CONNECTinvokeODBCConnect,hDlg.elseifax==IDM_DISCONNECTinvokeODBCDisconnect,hDlg.elseifax==IDM_QUERYinvokeRunQuery,hDlg.elseifax==IDM_CUSTOMQUERYinvokeDialogBoxParam,hInstance,IDD_QUERYDLG,hDlg,addrQueryProc,0.endif.endif.elsemoveax,FALSEret.endifmoveax,TRUEretDlgProcendpGetProgramPathprocinvokeGetModuleFileName,NULL,addrProgPath,sizeofProgPathstdmovedi,offsetProgPathaddedi,sizeofProgPath-1moval,"/"movecx,sizeofProgPathrepnescasbcldmovbyteptr[edi+2],0retGetProgramPathendpSwitchMenuStateprocFlag:DWORD.ifFlag==TRUEinvokeEnableMenuItem,hMenu,IDM_CONNECT,MF_GRAYEDinvokeEnableMenuItem,hMenu,IDM_DISCONNECT,MF_ENABLEDinvokeEnableMenuItem,hMenu,IDM_QUERY,MF_ENABLEDinvokeEnableMenuItem,hMenu,IDM_CUSTOMQUERY,MF_ENABLED.elseinvokeEnableMenuItem,hMenu,IDM_CONNECT,MF_ENABLEDinvokeEnableMenuItem,hMenu,IDM_DISCONNECT,MF_GRAYEDinvokeEnableMenuItem,hMenu,IDM_QUERY,MF_GRAYEDinvokeEnableMenuItem,hMenu,IDM_CUSTOMQUERY,MF_GRAYED.endifretSwitchMenuStateendpODBCConnectprochDlg:DWORDinvokeSQLAllocHandle,SQL_HANDLE_ENV,SQL_NULL_HANDLE,addrhEnv.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokeSQLSetEnvAttr,hEnv,SQL_ATTR_ODBC_VERSION,SQL_OV_ODBC3,0.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokeSQLAllocHandle,SQL_HANDLE_DBC,hEnv,addrhConn.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokelstrcpy,addrConnectString,addrstrConnectinvokelstrcat,addrConnectString,addrProgPathinvokelstrcat,addrConnectString,addrDBNameinvokeSQLDriverConnect,hConn,hDlg,addrConnectString,sizeofConnectString,addrConn,sizeofConn,addrStrLen,SQL_DRIVER_COMPLETE.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokeSwitchMenuState,TRUEinvokeMessageBox,hDlg,addrConn,addrConnectCaption,MB_OK+MB_ICONINFORMATION.elseinvokeSQLFreeHandle,SQL_HANDLE_DBC,hConninvokeSQLFreeHandle,SQL_HANDLE_ENV,hEnvinvokeMessageBox,hDlg,addrConnFail,addrAppName,MB_OK+MB_ICONERROR.endif.elseinvokeSQLFreeHandle,SQL_HANDLE_ENV,hEnvinvokeMessageBox,hDlg,addrAllocConnFail,addrAppName,MB_OK+MB_ICONERROR.endif.elseinvokeSQLFreeHandle,SQL_HANDLE_ENV,hEnvinvokeMessageBox,hDlg,addrSetAttrFail,addrAppName,MB_OK+MB_ICONERROR.endif.elseinvokeMessageBox,hDlg,addrAllocEnvFail,addrAppName,MB_OK+MB_ICONERROR.endifretODBCConnectendpODBCDisconnectprochDlg:DWORDinvokeSQLDisconnect,hConninvokeSQLFreeHandle,SQL_HANDLE_DBC,hConninvokeSQLFreeHandle,SQL_HANDLE_ENV,hEnvinvokeSwitchMenuState,FALSEinvokeShowWindow,hList,SW_HIDEinvokeMessageBox,hDlg,addrDisconnect,addrAppName,MB_OK+MB_ICONINFORMATIONretODBCDisconnectendpInsertColumnprocLOCALlvc:LV_COLUMNmovlvc.imask,LVCF_TEXT+LVCF_WIDTHmovlvc.pszText,offsetHeading1movlvc.lx,150invokeSendMessage,hList,LVM_INSERTCOLUMN,0,addrlvcmovlvc.pszText,offsetHeading2invokeSendMessage,hList,LVM_INSERTCOLUMN,1,addrlvcmovlvc.pszText,offsetHeading3invokeSendMessage,hList,LVM_INSERTCOLUMN,3,addrlvcretInsertColumnendpFillDataprocLOCALlvi:LV_ITEMLOCALrow:DWORDinvokeSQLBindCol,hStmt,1,SQL_C_CHAR,addrTheName,sizeofTheName,addrNameLengthinvokeSQLBindCol,hStmt,2,SQL_C_CHAR,addrTheSurname,sizeofTheSurname,addrSurnameLengthinvokeSQLBindCol,hStmt,3,SQL_C_CHAR,addrTelNo,sizeofTelNo,addrTelNoLengthmovrow,0.whileTRUEmovbyteptrds:[TheName],0movbyteptrds:[TheSurname],0movbyteptrds:[TelNo],0invokeSQLFetch,hStmt.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOmovlvi.imask,LVIF_TEXT+LVIF_PARAMpushrowpoplvi.iItemmovlvi.iSubItem,0movlvi.pszText,offsetTheNamepushrowpoplvi.lParaminvokeSendMessage,hList,LVM_INSERTITEM,0,addrlvimovlvi.imask,LVIF_TEXTinclvi.iSubItemmovlvi.pszText,offsetTheSurnameinvokeSendMessage,hList,LVM_SETITEM,0,addrlviinclvi.iSubItemmovlvi.pszText,offsetTelNoinvokeSendMessage,hList,LVM_SETITEM,0,addrlviincrow.else.break.endif.endwretFillDataendpRunQueryprochDlg:DWORDinvokeShowWindow,hList,SW_SHOWinvokeSendMessage,hList,LVM_DELETEALLITEMS,0,0invokeSQLAllocHandle,SQL_HANDLE_STMT,hConn,addrhStmt.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokeSQLExecDirect,hStmt,addrSQLStatement,sizeofSQLStatement.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokeFillData.elseinvokeShowWindow,hList,SW_HIDEinvokeMessageBox,hDlg,addrExecuteFail,addrAppName,MB_OK+MB_ICONERROR.endifinvokeSQLCloseCursor,hStmtinvokeSQLFreeHandle,SQL_HANDLE_STMT,hStmt.elseinvokeShowWindow,hList,SW_HIDEinvokeMessageBox,hDlg,addrAllocStmtFail,addrAppName,MB_OK+MB_ICONERROR.endifretRunQueryendpQueryProcprochDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD.ifuMsg==WM_CLOSEinvokeSQLFreeHandle,SQL_HANDLE_STMT,hStmtinvokeEndDialog,hDlg,0.elseifuMsg==WM_INITDIALOGinvokeShowWindow,hList,SW_SHOWinvokeSQLAllocHandle,SQL_HANDLE_STMT,hConn,addrhStmt.ifax==SQL_SUCCESS||ax==SQL_SUCCESS_WITH_INFOinvokelstrcpy,addrConn,addrSQLStatementinvokelstrcat,addrConn,addrWhereStatementinvokeSQLBindParameter,hStmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,25,0,addrSearchName,25,addrStrLeninvokeSQLPrepare,hStmt,addrConn,sizeofConn.elseinvokeShowWindow,hList,SW_HIDEinvokeMessageBox,hDlg,addrAllocStmtFail,addrAppName,MB_OK+MB_ICONERRORinvokeEndDialog,hDlg,0.endif.elseifuMsg==WM_COMMANDmoveax,wParamshreax,16.ifax==BN_CLICKEDmoveax,wParam.ifax==IDC_OKinvokeGetDlgItemText,hDlg,IDC_NAME,addrSearchName,25.ifax==0invokeMessageBox,hDlg,addrNoData,addrAppName,MB_OK+MB_ICONERRORinvokeGetDlgItem,hDlg,IDC_NAMEinvokeSetFocus,eax.elseinvokelstrlen,addrSearchNamemovStrLen,eaxinvokeSendMessage,hList,LVM_DELETEALLITEMS,0,0invokeSQLExecute,hStmtinvokeFillDatainvokeSQLCloseCursor,hStmt.endif.elseinvokeSQLFreeHandle,SQL_HANDLE_STMT,hStmtinvokeEndDialog,hDlg,0.endif.endif.elsemoveax,FALSEret.endifmoveax,TRUEretQueryProcendpendstart1 <script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: 数据库
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭