本帖最后由 gaoan000 于 2016-4-20 16:00 编辑
我想问个问题, 我在论坛上下了个UDF,运行起来也没错 ,但是在源码里我没看到为了读取数据而创建的记录集,请问下为什么呢?
就是下面这句,我看过您的连接教程也有这句吧,虽说现在可以用,但是还是想弄明白些 "$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $conn;""
发个函数源码,作者我忘记了Func _MSSQL_GetRecord($oConnectionObj, $sTable, $Columns = "*", $condition = "", $order = "")
Local $str, $quer, $aResult, $iColumns, $iRows
If IsObj($oConnectionObj) And Not @error Then
If IsArray($Columns) Then
If UBound($Columns, 2) - 1 <> 1 Then Return SetError(3, 0, 0)
$str = "SELECT '"
For $i = 1 To UBound($Columns) - 1
$str &= $Columns[$i] & "','"
Next
If $order = "" Then
$str = StringTrimRight($str, 2) & "FROM " & $sTable & " " & $condition & ";"
Else
$str = StringTrimRight($str, 2) & "FROM " & $sTable & " " & $condition & " ORDER BY " & $order & " ;"
EndIf
$quer = $oConnectionObj.execute($str)
With $quer
If Not .EOF Then
$aResult = .GetRows()
If IsArray($aResult) And UBound($aResult, 2) > 1 Then
$iColumns = UBound($aResult, 2)
$iRows = UBound($aResult)
ReDim $aResult[$iRows + 1][$iColumns]
For $x = $iRows To 1 Step -1
For $y = 0 To $iColumns - 1
$aResult[$x][$y] = $aResult[$x - 1][$y]
Next
Next
For $i = 0 To $iColumns - 1
$aResult[0][$i] = .Fields($i).Name
Next
ElseIf IsArray($aResult) And UBound($aResult, 2) = 1 Then
$iRows = UBound($aResult)
Local $bResult[$iRows + 1]
For $x = $iRows To 1 Step -1
$bResult[$x] = $aResult[$x - 1][0]
Next
$bResult[0] = $iRows
Return $bResult
Else
Return SetError(2, 0, 0)
EndIf
Else
Return SetError(4, $str, 0)
EndIf
EndWith
Return $aResult
Else
If $order = "" Then
;~ If $Columns = "*" Then
$quer = $oConnectionObj.execute("SELECT " & $Columns & " FROM " & $sTable & " " & $condition & ";")
;~ Else
;~ $quer = $oConnectionObj.execute("SELECT '" & $Columns & "' FROM " & $sTable & " " & $condition & ";")
;~ EndIf
Else
;~ If $Columns = "*" Then
$quer = $oConnectionObj.execute("SELECT " & $Columns & " FROM " & $sTable & " " & $condition & " ORDER BY " & $order & " ;")
;~ Else
;~ $quer = $oConnectionObj.execute("SELECT '" & $Columns & "' FROM " & $sTable & " " & $condition & " ORDER BY " & $order & " ;")
;~ EndIf
EndIf
If Not $quer.EOF Then
$aResult = $quer.GetRows()
If IsArray($aResult) And UBound($aResult, 2) > 1 Then
$iColumns = UBound($aResult, 2)
$iRows = UBound($aResult)
ReDim $aResult[$iRows + 1][$iColumns]
For $x = $iRows To 1 Step -1
For $y = 0 To $iColumns - 1
$aResult[$x][$y] = $aResult[$x - 1][$y]
Next
Next
For $i = 0 To $iColumns - 1
$aResult[0][$i] = $quer.Fields($i).Name
Next
ElseIf IsArray($aResult) And UBound($aResult, 2) = 1 Then
$iRows = UBound($aResult)
Local $bResult[$iRows + 1]
For $x = $iRows To 1 Step -1
$bResult[$x] = $aResult[$x - 1][0]
Next
$bResult[0] = $iRows
Return $bResult
Else
Return SetError(2, 0, 0)
EndIf
Else
Return SetError(4, $str, 0)
EndIf
Return $aResult
EndIf
EndIf
Return SetError(1, 0, 0)
EndFunc ;==>_MSSQL_GetRecord
|