找回密码
 加入
搜索
查看: 3027|回复: 2

[效率算法] 连接MSSQL数据库的错误机制

[复制链接]
发表于 2011-12-20 13:12:40 | 显示全部楼层 |阅读模式
在“已解决问题”分区看到了这个帖子,可是没有看到贴上来的答案,所以再来请教。

最近写个东西,希望检查数据库是否正确,请大侠给个例子
#RequireAdmin
Opt("TrayMenuMode", 1)
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("初始设置", 246, 208, 284, 184)
$dblocal = _GUICtrlIpAddress_Create($Form1, 80, 24, 145, 17, BitOR($WS_TABSTOP, $WS_VISIBLE, $WS_CHILD, $WS_CLIPSIBLINGS), 0)
_GUICtrlIpAddress_Set($dblocal, "127.0.0.1")
$dbname = GUICtrlCreateInput("JMSYS", 80, 56, 145, 21)
$dbpas = GUICtrlCreateInput("chinanip", 80, 128, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
$Label1 = GUICtrlCreateLabel("数据库地址", 14, 25, 64, 17)
$Label2 = GUICtrlCreateLabel("数据库名称", 13, 59, 64, 17)
$Label3 = GUICtrlCreateLabel("数据库密码", 14, 131, 64, 17)
$save = GUICtrlCreateButton("确认保存", 75, 170, 81, 25)
$dbuser = GUICtrlCreateInput("sa", 80, 93, 145, 21)
$Label4 = GUICtrlCreateLabel("用 户 名", 18, 96, 62, 17)
If Not FileExists(@MyDocumentsDir & "\EpiData\settings.in") Then
        DirCreate(@MyDocumentsDir & "EpiData")
        GUISetState(@SW_SHOW, $Form1)
Else
        
EndIf
Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling.

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $save
                        $db_local = _GUICtrlIpAddress_Get($dblocal)
                        $db_user = GUICtrlRead($dbuser)
                        $db_name = GUICtrlRead($dbname)
                        $db_pas = GUICtrlRead($dbpas)
                        $addfld = ObjCreate("ADODB.Connection")
                        $addfld.Open('"driver={SQL Server};server=' & $db_local & ';uid=' & $db_user & ';pwd=' & $db_pas & ';database=' & $db_name & '"')
                        $oMyError = ObjEvent($addfld.Error, "MyErrFunc") ; Install a custom error handler
                        If $g_eventerror Then
                                $g_eventerror = 0 
                                MsgBox(0, "数据库连接错误", "不能连接到数据库!请检查数参数!" & @CRLF & $oMyError.windescription)
                        Else
                                If Not IniWrite(@MyDocumentsDir & "EpiData\settings.ini", "常规", "时间", @YEAR & "-" & @MON & "-" & @MDAY) Then
                                        MsgBox(0, "设置保存异常", "未能正确保存设置,请重试!" & @CRLF & @MyDocumentsDir & "EpiData\settings.ini")
                                Else
                                        IniWrite(@MyDocumentsDir & "EpiData\settings.ini", "常规", "保存者", @UserName)
                                        $file = FileOpen(@MyDocumentsDir & "EpiData\settings.ini", 1)
                                        FileWriteLine(@MyDocumentsDir & "EpiData\settings.ini", @CRLF)
                                        FileClose($file)
                                        IniWrite(@MyDocumentsDir & "EpiData\settings.ini", "数据库", "地    址", $db_local)
                                        IniWrite(@MyDocumentsDir & "EpiData\settings.ini", "数据库", "用 户 名", $db_user)
                                        IniWrite(@MyDocumentsDir & "EpiData\settings.ini", "数据库", "数据库名", $db_name)
                                        IniWrite(@MyDocumentsDir & "EpiData\settings.ini", "数据库", "用户密码", $db_pas)
                                EndIf
                        EndIf
        EndSwitch
        AdlibRegister("detect", 3000)
WEnd
Func MyErrFunc()
        $HexNumber = Hex($oMyError.number, 8)
        $g_eventerror = 1
EndFunc   ;==>MyErrFunc
发表于 2011-12-20 17:19:59 | 显示全部楼层
学习学习,还没开始玩SQL,都在ACC
发表于 2011-12-25 10:36:19 | 显示全部楼层
又是一个好工具,谢谢共享
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-1 07:40 , Processed in 0.074983 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表