找回密码
 加入
搜索
查看: 2346|回复: 5

[AU3基础] [已解决]请教个问题。关于数据库查询

  [复制链接]
发表于 2012-2-1 21:13:01 | 显示全部楼层 |阅读模式
本帖最后由 xyhqqaa 于 2012-4-10 08:29 编辑

新手求助,,刚接触数据库。。。。比如,我新建数据库查询资料、、。。我的想法是单击输入框。。但是在输入框输入相关信息后需要回车后才在listview中显示。清空输入框的同时清空listview。。。现在遇到的问题是。我在输入框输入数字的时候,就自动已经在listview显示数据了。。。而且是重复不间断输出重复信息。。。。。。。偶尔会卡死。。。囧 。。代码大概如下
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#Region ### START Koda GUI section ### Form=
Dim $ServerIP = '*******', $ServerID = 'sa', $ServerPwd = '', $ServerDatabase = '****'
$Form1 = GUICreate("Form1", 348, 229, 331, 226)
$Input1=GUICtrlCreateInput("", 3, 8, 342, 21)
$ListView1 = GUICtrlCreateListView("工号 |姓名 |部门       ", 3, 32, 225, 193)
$ListView2 = GUICtrlCreateListView("账号         ", 231, 32, 113, 193)
Dim $AccelKeys[2][2] = [["{Enter}", $Input1]]                
GUISetAccelerators($AccelKeys)
GUISetState()
                        
#EndRegion ### END Koda GUI section ###
AdlibRegister("_del")

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $GUI_EVENT_PRIMARYUP
                        $CursorInfo = GUIGetCursorInfo()
                        If $CursorInfo[4] = 3 Then GUICtrlSetData($Input1, "")
           Case $nMsg = $Input1
             _read()
                        
        EndSwitch
WEnd
Func _del()
        $number = GUICtrlRead($Input1)
        If $number = "" Then
                _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView1))
        EndIf
EndFunc 

Func _read()
        $number = GUICtrlRead($Input1)
        If $number <> "" Then
                $conn = ObjCreate("ADODB.Connection")
                $RS = ObjCreate("ADODB.Recordset")
                $conn.Open("driver={SQL Server};server=" & $ServerIP & ";uid=" & $ServerID & ";pwd=" & $ServerPwd & ";database=" & $ServerDatabase)
                $RS.ActiveConnection = $conn
        $RS.Open("SELECT *, *, SP FROM * WHERE (S61BH LIKE '%" & $number & "%') OR (SP LIKE '%" & $number & "%') order by * desc")
                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
                $conn.close
        EndIf
EndFunc   ;==>_read

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2012-2-1 22:29:51 | 显示全部楼层
你這樣的作法 不太正確喔....老實說我看不懂你要表達的意思.....但是從程式的角度  有幾個問題 可以提供你參考

首先 您把
                $conn = ObjCreate("ADODB.Connection")
                $RS = ObjCreate("ADODB.Recordset")
                $conn.Open("driver={SQL Server};server=" & $ServerIP & ";uid=" & $ServerID & ";pwd=" & $ServerPwd & ";database=" & $ServerDatabase)
                $RS.ActiveConnection = $conn

這些都放到 自訂的函數裡面  意思是 只要讀取到 這個函數 就必須要連線依次資料庫 讀取完畢後 在關閉資料庫......

為何 不要放到主程式就好  自訂的函數裡面只要放查詢語法.....也就是 主程式 一打開 就連線資料庫.....主程式 關閉的時候  關閉資料庫連線......這樣一來  連線只要一次.......

至於 你的不斷重複  我想是 跟 下面的這段語法的方式有關......
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $GUI_EVENT_PRIMARYUP
                        $CursorInfo = GUIGetCursorInfo()
                        If $CursorInfo[4] = 3 Then GUICtrlSetData($Input1, "")
                Case $nMsg = $Input1
                       _read()
        EndSwitch
WEnd

我不太會說是哪裡的問題........但是 是不是 可以將上面的那段程式 改為........ 先不耀清空输入框的同时清空listview。。

改為 輸入完畢以後再去清空  看看是不是 還是會不斷的顯示
发表于 2012-2-1 23:07:22 | 显示全部楼层
这个很容易明白,input发生事件,_read()就运行,很正常,lz一直在输入,肯定一直运行
发表于 2012-2-1 23:32:26 | 显示全部楼层
所以 不能採用 事件模式 去做判斷嚕
发表于 2012-2-2 03:35:08 | 显示全部楼层
回复 4# kk_lee69
倒也未必,看lz要什么效果
发表于 2012-2-2 09:21:53 | 显示全部楼层
Input控件并非一直都有GUIGetMsg消息,它本身就只响应回车。
所以第 13、14行纯属多余。
关键仔细看下第 28 行。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-29 07:29 , Processed in 0.104949 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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