本帖最后由 luqiwe 于 2014-4-14 16:12 编辑
最近在写一网站网页,需要从后台ASP网页查询到的MYSQL记录集返回给前台ASP网页,我们知道AJAX是无力从后台返回数据库记录集给前台网页的.查阅大量资料,就目前而言记录集转换成JSON格式流,再由前台VBA导入WEBoffice控件的excel是个不错的选择.经过些思考,现将function过程代码奉献给大家.
Function GetJSON(Rs)
Dim JSON
dim returnStr
dim i
dim oneRecord
if Rs.eof=false and Rs.Bof=false then
returnStr="{ "&chr(34)&"records"&chr(34)&":["
while Rs.eof=false
for i=0 to Rs.Fields.Count -1
oneRecord=oneRecord & chr(34) & Rs.Fields(i).Name & chr(34) &":"
oneRecord=oneRecord & chr(34) & Rs.Fields(i).Value & chr(34) &","
Next
oneRecord=left(oneRecord,InStrRev(oneRecord,",")-1)
oneRecord=oneRecord & "},"
returnStr=returnStr & oneRecord
Rs.MoveNext
Wend
returnStr=left(returnStr,InStrRev(returnStr,",")-1)
returnStr=returnStr & "]}"
end if
GetJSON=returnStr
End Function