#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