#include <Access.au3>
#include 'MSSQL.au3'
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $iIP = '127.0.0.1,1220' ; IP地址
Global $sUserName = 'sa' ; 连接帐号
Global $sPassWord = 'admin' ; 连接密码
Global $sDatabase = 'MYDB' ; 数据库名
Global $sTableName = 'TestTable1' ; 表名称
$sqlCon = _MSSQL_Con($iIP, $sUserName, $sPassWord, $sDatabase)
GUICreate('MSSQL - 数据库操作例子', 800, 600)
$ListView1 = GUICtrlCreateListView('ID|第一列|第二列|第三列|第四列|第五列', 5, 5, 790, 540, -1, _
BitOR($WS_EX_CLIENTEDGE, $LVS_EX_FULLROWSELECT, $LVS_REPORT))
_GUICtrlListView_SetColumnWidth($ListView1, 0, 130)
_GUICtrlListView_SetColumnWidth($ListView1, 1, 130)
_GUICtrlListView_SetColumnWidth($ListView1, 2, 130)
_GUICtrlListView_SetColumnWidth($ListView1, 3, 130)
_GUICtrlListView_SetColumnWidth($ListView1, 4, 130)
_GUICtrlListView_SetColumnWidth($ListView1, 5, 130)
$hImage = _GUIImageList_Create(1, 25)
_GUICtrlListView_SetImageList($ListView1, $hImage, 1)
$Button1 = GUICtrlCreateButton('创建表', 10, 560, 100, 30, 0)
$Button2 = GUICtrlCreateButton('写入数据', 130, 560, 100, 30, 0)
$Button3 = GUICtrlCreateButton('读取数据', 250, 560, 100, 30, 0)
$Button4 = GUICtrlCreateButton('更新数据', 370, 560, 100, 30, 0)
$Button5 = GUICtrlCreateButton('删除数据', 490, 560, 100, 30, 0)
$Button6 = GUICtrlCreateButton('查找数据', 610, 560, 100, 30, 0)
GUISetState()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
; 关闭数据库连接
_MSSQL_End($sqlCon)
Exit
Case $Button1
; 在数据库中创建表的同时创建相应的列
_MSSQL_CreateTable($sqlCon, $sTableName, False, 'ID char(17),第一列 Text,第二列 Text,第三列 Text,第四列 Text,第五列 Text')
Case $Button2;写入
Local $aValue[6]
$aValue[0] = (@YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC)
$aValue[1] = '添加数据 - ' & Random(100, 999, 1)
$aValue[2] = '添加数据 - ' & Random(100, 999, 1)
$aValue[3] = '添加数据 - ' & Random(100, 999, 1)
$aValue[4] = '添加数据 - ' & Random(100, 999, 1)
$aValue[5] = '添加数据 - ' & Random(100, 999, 1)
$sText = _ArrayToString($aValue)
GUICtrlCreateListViewItem($sText, $ListView1)
_MSSQL_AddRecord($sqlCon, $sTableName, $aValue)
Case $Button3 ;读取
_MYSQL_ReadDataToList($sqlCon, $sTableName)
Case $Button5 ;删除
$iIndex = GUICtrlRead($ListView1)
If $iIndex Then
$sItemText = GUICtrlRead($iIndex)
$iID = StringRegExpReplace($sItemText, '\|.+', '')
GUICtrlDelete($iIndex)
_MSSQL_DeleteRecord($sqlCon, $sTableName, $iID, 'ID')
EndIf
Case $Button4 ;更新
$iIndex = GUICtrlRead($ListView1)
If $iIndex Then
$sItemText = GUICtrlRead($iIndex)
$iID = StringRegExpReplace($sItemText, '\|.+', '')
GUICtrlDelete($iIndex)
$iNumber = (@YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC)
;~ _MSSQL_UpdateRecord($sqlCon, $sTableName, "ID", $sValue, "WHere ID = '" & $iID & "'")
Local $aColumn[6] ; 需要更新数据的列名,位置与下面的更新的值对齐,数量必须相同。
$aColumn[0] = 'ID'
$aColumn[1] = '第一列'
$aColumn[2] = '第二列'
$aColumn[3] = '第三列'
$aColumn[4] = '第四列'
$aColumn[5] = '第五列'
Local $aNewValue[6] ; 新的数据,位置与上面的列对齐,数量必须相同。
$aNewValue[0] = $iNumber
$aNewValue[1] = '更新数据 - ' & Random(100, 999, 1)
$aNewValue[2] = '更新数据 - ' & Random(100, 999, 1)
$aNewValue[3] = '更新数据 - ' & Random(100, 999, 1)
$aNewValue[4] = '更新数据 - ' & Random(100, 999, 1)
$aNewValue[5] = '更新数据 - ' & Random(100, 999, 1)
;~ $aNewValue[1] = Random(100, 9999, 1)
;~ $aNewValue[2] = Random(100, 9999, 1)
;~ $aNewValue[3] = Random(100, 9999, 1)
;~ $aNewValue[4] = Random(100, 9999, 1)
;~ $aNewValue[5] = Random(100, 9999, 1)
_MSSQL_UpdateRecordEx($sqlCon, $sTableName, $aColumn, $aNewValue, $iID)
_MYSQL_ReadDataToList($sqlCon, $sTableName)
EndIf
Case $Button6 ;查找
$aFindData = _MSSQL_GetRecord($sqlCon, $sTableName, '第五列', 'WHere ID = ' & "'580'")
_ArrayDisplay($aFindData)
EndSwitch
WEnd
Func _MYSQL_ReadDataToList($sqlCon, $sTable)
Local $sText
_GUICtrlListView_BeginUpdate($ListView1)
GUICtrlSendMsg($ListView1, 4105, 0, 0)
$aRecord = _MSSQL_GetRecord($sqlCon, $sTable)
For $i = 1 To UBound($aRecord) - 1
For $z = 0 To 5
$sText &= $aRecord[$i][$z] & '|'
Next
GUICtrlCreateListViewItem($sText, $ListView1)
$sText = ''
Next
_GUICtrlListView_EndUpdate($ListView1)
EndFunc ;==>_MYSQL_ReadDataToList