本帖最后由 xyhqqaa 于 2017-9-14 08:14 编辑 $DB_Provider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & @ScriptDir & "\fdeam.mdb"
$DB_ado = ObjCreate("ADODB.Connection")
$DB_ado.Open($DB_Provider)
$sqlstr &= " and format(购置日期,'yyyymmdd') BETWEEN '"&_datefomat(GUICtrlRead($Date1))&"' AND '"&_datefomat(GUICtrlRead($Date2))&"'"
$DB_rec = _SelectData($sqlStr)
If Not IsArray($DB_rec) Then
MsgBox(48,"提示!","没有符合要求的记录,请更改查询条件!")
Return False
EndIf
(上面是从中摘录,代码太长,怕看起来乱糟糟的)
Func _SelectData($sqlstr) ;查询数据库 ,返回数组,宏保存记录个数
Local $ret="null"
$RS = ObjCreate("ADODB.Recordset")
With $RS
.Source=$sqlstr
.ActiveConnection=$DB_ado
EndWith
$RS.Open ()
If $Rs.bof And $Rs.eof Then
SetError(1)
Else
SetError(0)
$ret=$RS.GetRows
EndIf
$Rs.close()
Return $ret
EndFunc
$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(5).value & "|" & $RS.Fields(4).value & "|" & $RS.Fields(7).value, $ListView1)
$RS.movenext
WEnd
$RS.close
$addfld.Close
以上是两段数据库连接的代码,我在论坛看到的例子,我想请问大哥们,这两段代码功能差异在哪。第一段代码很简洁,这样写好吗。菜鸟求指教 |