au3链接mssql数据库

;以下为连接数据库mssql
$conn = ObjCreate("ADODB.Connection")
$RS = ObjCreate("ADODB.Recordset")

$conn.Open ("driver={SQL Server};server="&$Server&";uid="&$user&";pwd="&$pwd&";database="&$dbs);
;主机名IP127.0.0.1,UID=用户,PWD=密码,database=数据库名  
$RS.ActiveConnection = $conn ; 设定RS 是 conn 的集合 重要的部份

au3操作mssql数据库

;操作 sql
;标准的 读取数据 循环模式语法
;几乎 所有的查询都只要套用这个模式就可以了......
$RS.open ("select * from table");
While (Not $RS.eof And Not $RS.bof);循环读表 有资料就跑回圈  没资料就会跳出
        MsgBox(0, "字段一", $RS.Fields (0).value,5);
        ;显示第一个字段 资料 ,AU3返回的数据通常 都是不含 原本的 字段名称  要取的字段名称需要其它方法 
        MsgBox(0, "字段二", $RS.Fields (1).value);显示第二个字段 资料 
        If StringIsDigit($RS.Fields(1).value)=0 Then   ;这边可以加上判断
        EndIf
        $RS.movenext;下一笔资料
WEnd;结束循环读表
$RS.close;关闭数据分表 当使用 RS 查询之后 整个纪录读取完毕后  必须要关闭 RS

au3操作mssql数据库实例2

Func fixcard($cardno, $nType, $CardBalance)
    $server = "192.168.1.100"
    $user = "root"
    $pwd = "123456"
    $dbs = "testdb"
    $conn = ObjCreate("ADODB.Connection")
    $RS = ObjCreate("ADODB.Recordset")

    $conn.Open("driver={SQL Server};server=" & $server & ";uid=" & $user & ";pwd=" & $pwd & ";database=" & $dbs) ;主机名IP127.0.0.1,UID=用户,PWD=密码,database=数据库名
    $RS.ActiveConnection = $conn ; 设定RS 是 conn 的集合 重要的部份
    ;$conn.close; 关闭数据库连接

    ;建立数据库  连接


    $RS.open("select * from Card where cardno='" & $cardno &"'");& $iden_id)

    $cardstatus = $RS.Fields(0).value ;if value<>1 then fix it to 1
    $ntype_n = $RS.Fields(1).value
    $cardbalance_n = $RS.Fields(2).value ; temp card=0
    $empname = $RS.Fields(3).value
    ;MsgBox(0,"系统查到的状态",$cardstatus)
    $RS.close
    ;MsgBox(0,"",StringLen($empname))
    ;    $empname='不存在'

    $goon = MsgBox(292, "", "姓名:" & $empname & @CRLF & " cardbalance:" & $cardbalance_n & @CRLF & "是否继续?")
    If $goon <> 6 Or StringLen($empname) < 2 Then
        MsgBox(16, "", "Error 401")
    Else

        $conn.Execute("update Card set CardStatus=1 where cardno='" & $cardno & "'") ;1正常状态 
        $conn.close
        MsgBox(0, "ok", "好了!")
    EndIf

EndFunc 

au3查询mssql后将结果写入listview展示(未验证)

Dim $Server = "DBSERVERSQL"
Dim $Company = "Z100A01001"
DIM $array[1][1]

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <Date.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

#RequireAdmin

$WinMain = GuiCreate('TEST',800,600)

$menu2 = GuiCtrlCreateButton('RUN', 210, 30, 150, 30); 
GUICtrlSetFont(-1,10)

$ListView1 = GUICtrlCreateListView("a  |b  |c   |d  |e  |f  |g  ", 32, 100, 705, 401)


$conn = ObjCreate("ADODB.Connection")
$RS = ObjCreate("ADODB.Recordset")
$conn.Open ("driver={SQL Server};server="&$Server&";uid=sa;pwd=123456;database="&$Company);
$RS.ActiveConnection = $conn;

GuiSetState()
; 顯示視窗


While 1

        Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $menu2
                        $RS.open ("SELECT COUNT(*) FROM INVLA Where LA010 like 'AMB%'");      
                        While (Not $RS.eof And Not $RS.bof);
                                $P=$RS.Fields(0).value
                                $RS.movenext;
                        WEnd;
                        $RS.close;


                        Dim $APP[$P][7]                   
                        $i=0 
                        $iTimer = TimerInit()
                        $RS.open ("SELECT  LA001,LA004,LA006,LA007,LA008,LA009,LA010 FROM INVLA Where LA010 like 'AMB%'");

                        While (Not $RS.eof And Not $RS.bof);
                                $APP[$i][0]=$RS.Fields(0).value
                                $APP[$i][1]=$RS.Fields(1).value
                                $APP[$i][2]=$RS.Fields(2).value
                                $APP[$i][3]=$RS.Fields(3).value
                                $APP[$i][4]=$RS.Fields(4).value
                                $APP[$i][5]=$RS.Fields(5).value
                                $APP[$i][6]=$RS.Fields(6).value

                                $i=$i+1

                                ;GUICtrlSetState($SETDATA,$GUI_ENABLE)
                                $RS.movenext;
                        WEnd;
                        $RS.close;
                        _GUICtrlListView_AddArray($ListView1, $APP)

            MsgBox(4160, "Information", "Load time: " & TimerDiff($iTimer) / 1000 & " seconds")                      
        EndSwitch
WEnd
最后编辑: admin  文档更新时间: 2021-03-16 21:53   作者:admin