#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=..\..\..\..\WINDOWS\system32\SHELL32.dll|-45
#AutoIt3Wrapper_outfile=..\数据库.exe
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 442, 192, 124)
$Input1 = GUICtrlCreateInput("", 10, 410, 121, 21)
$Input2 = GUICtrlCreateInput("", 135, 410, 121, 21)
$Input3 = GUICtrlCreateInput("", 260, 410, 121, 21)
$Input4 = GUICtrlCreateInput("无", 385, 410, 121, 21)
$Button1 = GUICtrlCreateButton("添加", 530, 410, 75, 25)
$Input5 = GUICtrlCreateInput("", 10, 325, 121, 21)
$Input6 = GUICtrlCreateInput("", 135, 325, 121, 21)
$Input7 = GUICtrlCreateInput("", 260, 325, 121, 21)
$Input8 = GUICtrlCreateInput("", 385, 325, 121, 21)
$Button2 = GUICtrlCreateButton("修改", 530, 325, 75, 25)
$Input9 = GUICtrlCreateInput("", 10, 365, 121, 21)
$Button3 = GUICtrlCreateButton("搜索", 140, 365, 75, 25)
$ListView1 = GUICtrlCreateListView("编号|姓名|工号|电话号码|备注", 5, 15, 611, 271)
$Button4 = GUICtrlCreateButton("删除", 230, 365, 75, 25)
$Button5 = GUICtrlCreateButton("创建", 315, 365, 75, 25)
$Button6 = GUICtrlCreateButton("读取", 405, 365, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$mdb_data_path="sky.mdb"
$mdb_data_pwd="123"
$T="*"
$tblname="tywb"
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button5
data($mdb_data_path, $tblname)
Case $Button1
xieru()
du()
Case $Button2
xiugai()
du()
Case $Button6
du()
Case $Button4
del()
du()
Case $Button3
;up()
EndSwitch
WEnd
Func xieru()
If GUICtrlRead($Input1)<>'' And GUICtrlRead($Input2)<>'' And GUICtrlRead($Input3)<>'' And GUICtrlRead($Input4)<>'' Then
$name =GUICtrlRead($Input1)
$gonghao =GUICtrlRead($Input2)
$tel=GUICtrlRead($Input3)
$beizhu=GUICtrlRead($Input4)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$addfld.Execute("insert into tywb (name,gonghao,tel,beizhu) values('"&$name&"','"&$gonghao&"','"&$tel&"','"&$beizhu&"')")
$addfld.close
MsgBox(4096, "提示:"," 成功写入数据库!")
Else
MsgBox(0,'','写入失败')
EndIf
EndFunc
Func xiugai()
If GUICtrlRead($Input5)<>'' And GUICtrlRead($Input6)<>'' And GUICtrlRead($Input7)<>'' And GUICtrlRead($Input8)<>'' Then
$name1 =GUICtrlRead($Input5)
$gonghao1 =GUICtrlRead($Input6)
$tel1=GUICtrlRead($Input7)
$beizhu1=GUICtrlRead($Input8)
$Strn=GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin=StringSplit($Strn,"|")
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &$mdb_data_path& ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$sQuery ="update " & $tblname & " set name='"&$name1&"',gonghao='"&$gonghao1&"',tel='"&$tel1&"',beizhu='"&$beizhu1&"' WHERE id" & " = " & $Strnspin[1]
; MsgBox(0,"",$sQuery)
$addfld.execute($sQuery)
$addfld.close
MsgBox(4096, "提示:",$Strnspin[1]&" 更新成功!!!")
Else
MsgBox(4096, "提示:"," 更新失败!!!")
EndIf
EndFunc
Func du()
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &$mdb_data_path& ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open ("Select "&$T & " From " & $tblname )
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&"|"& $RS.Fields (3).value&"|"& $RS.Fields (4).value,$ListView1 )
; MsgBox(0, "", $RS.Fields (1).value);显示表第一个数据
$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& ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$sQuery ="DELETE FROM " & $tblname & " IN '" & $mdb_data_path & "' WHERE id" & " = " & $Strnspin[1]
; MsgBox(0,"",$sQuery)
$addfld.execute($sQuery)
$addfld.close
MsgBox(4096, "提示:",$Strnspin[1]&" 删除成功!!!")
EndFunc
Func data($mdb_data_path, $tblname)
If Not FileExists($mdb_data_path) Then
$newMdb = ObjCreate('ADOX.Catalog')
$newMdb.Create("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$newmdb.ActiveConnection.Close;建数据库
$addtbl = ObjCreate("ADODB.Connection")
$addTbl.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$addTbl.Execute("CREATE TABLE " & $tblname)
$addtbl.Close;建表
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$addfld.Execute("ALTER TABLE " & $tblname & " ADD id identity(1, 1) primary key,name char ,gonghao int,tel int,beizhu char");id identity(1, 1) primary key为自动编号和主键
$addfld.Close;建列表名
MsgBox(64,"提示","建立数据库成功",5)
Else
MsgBox(64,"提示","你已经建立了一个数据库了",5)
du()
EndIf
EndFunc
请教朋友们一个问题,关于access数据库的读,写,修改都已经没有问题,我现在无法实现搜索,请哪位朋友找个例子参考一下,我想要的效果是:输入姓名或者是工号都可以找到记录并把结果显示在ListView,请朋友们不吝指教。