#NoTrayIcon
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
#include <Access.au3>
$Form1 = GUICreate("Form1", 400, 381, 290, 255)
$Label1 = GUICtrlCreateLabel("用户名", 30, 8, 45, 17)
$Label2 = GUICtrlCreateLabel("密码", 30, 32, 40, 17)
$Input1 = GUICtrlCreateInput("", 82, 5, 121, 21)
$Input2 = GUICtrlCreateInput("", 82, 29, 121, 21)
$Button1 = GUICtrlCreateButton("创建", 12, 52, 75, 25)
$Button2 = GUICtrlCreateButton("添加", 92, 52, 75, 25)
$ListView1 = GUICtrlCreateListView("编号|姓名|密码", 5, 80, 210, 220,BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
GUICtrlSendMsg(-1, 0x101E, 0, 45)
GUICtrlSendMsg(-1, 0x101E, 1, 80)
GUICtrlSendMsg(-1, 0x101E, 2, 80)
$Button3 = GUICtrlCreateButton("查看记录", 5, 305, 75, 25)
$Button8 = GUICtrlCreateButton("删除记录", 95, 305, 75, 25)
$Label3 = GUICtrlCreateLabel("用户名", 220, 8, 45, 17)
$Label4 = GUICtrlCreateLabel("密码", 220, 32, 40, 17)
$Input3 = GUICtrlCreateInput("",270, 5, 121, 21)
$Input4 = GUICtrlCreateInput("",270, 29, 121, 21)
$Button4 = GUICtrlCreateButton("用户名查询", 212, 52, 75, 25)
$Button5 = GUICtrlCreateButton("密码查询", 292, 52, 75, 25)
$Label3 = GUICtrlCreateLabel("用户名", 220, 100, 45, 17)
$Label4 = GUICtrlCreateLabel("密码", 220, 132, 40, 17)
$Input3 = GUICtrlCreateInput("",270, 100, 121, 21)
$Input4 = GUICtrlCreateInput("",270, 129, 121, 21)
$Button6 = GUICtrlCreateButton("读取选中项", 222, 160, 75, 25)
$Button7 = GUICtrlCreateButton("保存修改", 302, 160, 75, 25)
GUISetState(@SW_SHOW)
$mdb_data_path = @ScriptDir & "\tets.mdb" ;脚本所在目录下生成名为test。mdb的数据库
$adTable = "users" ;表名称
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
If Not FileExists($mdb_data_path) Then
$newMdb = ObjCreate("ADOX.Catalog")
$newMdb.Create("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$newmdb.ActiveConnection.Close
$addtbl = ObjCreate("ADODB.Connection")
$addTbl.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$addTbl.Execute("CREATE TABLE " & $adTable)
$addtbl.Close
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$addfld.Execute("ALTER TABLE " & $adTable & " ADD id identity(1, 1) primary key,name text(255) ,pass text(255)")
$addfld.Close
MsgBox(64,"提示","建立数据库成功",5)
Else
MsgBox(64,"提示","你已经建立了一个数据库了",5)
EndIf
Case $Button2
If GUICtrlRead($Input1) <> '' And GUICtrlRead($Input2) <> '' Then
$ipt1 = GUICtrlRead($Input1)
$ipt2 = GUICtrlRead($Input2)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$addfld.Execute("insert into "&$adTable&" (name,pass) values('"&$ipt1&"','"&$ipt2&"')")
$addfld.close
MsgBox(48,'提示','添加记录成功!')
GUICtrlSetData($Input1,'')
GUICtrlSetData($Input2,'')
GUICtrlSetState($Input1,$GUI_FOCUS)
Else
MsgBox(48,'提示','用户名和密码错误')
GUICtrlSetState($Input1,$GUI_FOCUS)
EndIf
Case $Button3
read()
Case $Button4
$ipt3 = GUICtrlRead($Input3)
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection =$addfld
$RS.Open ("Select * From "& $adTable & " WHERE name" & " ='"&$ipt3&"'")
while Not $RS.eof And Not $RS.bof
if @error =1 Then ExitLoop
GUICtrlCreateListViewItem ( $RS.Fields (0).value&"|"& $RS.Fields (1).value&"|"& $RS.Fields (2).value,$ListView1 )
$rs.movenext
WEnd
$rs.close
$addfld.Close
Case $Button5
$ipt4 = GUICtrlRead($Input4)
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection =$addfld
$RS.Open ("Select * From "& $adTable & " WHERE pass" & " ='"&$ipt4&"'")
while Not $RS.eof And Not $RS.bof
if @error =1 Then ExitLoop
GUICtrlCreateListViewItem ( $RS.Fields (0).value&"|"& $RS.Fields (1).value&"|"& $RS.Fields (2).value,$ListView1 )
$rs.movenext
WEnd
$rs.close
$addfld.Close
Case $Button6
$Strn=GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin=StringSplit($Strn,"|")
If $Strnspin[1]='0' Then
MsgBox(48,'提示','请选择要更新的条目')
Else
dataup()
EndIf
Case $Button7
back()
Case $Button8
del()
EndSwitch
WEnd
Func dataup()
$Strn=GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin=StringSplit($Strn,"|")
GUICtrlSetData($Input3,'')
GUICtrlSetData($Input4,'')
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open ("Select * From " & $adTable & " WHERE id" & " = " & $Strnspin[1])
GUICtrlSetData($Input3,$RS.Fields (1).value)
GUICtrlSetData($Input4,$RS.Fields (2).value)
$rs.movenext
$rs.close
$addfld.Close
EndFunc
Func back()
If GUICtrlRead($Input3)<>'' And GUICtrlRead($Input4)<>'' Then
$read1=GUICtrlRead($Input3)
$read2=GUICtrlRead($Input4)
$Strn=GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin=StringSplit($Strn,"|")
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$addfld.execute("update " & $adTable & " set name='"&$read1&"',pass='"&$read2&"' WHERE id" & " = " & $Strnspin[1])
$addfld.close
GUICtrlSetData($Input3,'')
GUICtrlSetData($Input4,'')
read()
Else
MsgBox(48,'出错啦','请检查输入是否完整')
EndIf
EndFunc
Func read()
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open ("Select * From " & $adTable )
while Not $RS.eof And Not $RS.bof
if @error =1 Then ExitLoop
GUICtrlCreateListViewItem ( $RS.Fields (0).value&"|"& $RS.Fields (1).value&"|"& $RS.Fields (2).value,$ListView1 )
$rs.movenext
WEnd
$rs.close
$addfld.Close
EndFunc
Func del()
$Strn=GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin=StringSplit($Strn,"|")
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$sQuery ="DELETE FROM " & $adTable & " IN '" & $mdb_data_path & "' WHERE id" & " = " & $Strnspin[1]
$addfld.execute($sQuery)
$addfld.close
read()
EndFunc