奇怪的mysql结果在asp

时间:2012-06-01 08:08:31

标签: mysql asp-classic

我有以下代码:

strSQL = "SELECT COUNT(*) AS anz FROM tbl_ergebnisse WHERE testavg >0"
set rs3 = Conn.Execute(strSQL)
Response.Write "Count(*): <br>" 
if not rs3.eof then
    Response.Write "Anz: " & rs3("anz")
else
    Response.Write "EOF" 
end if

strSQL = "SELECT testavg FROM tbl_ergebnisse WHERE testavg >0"
set rs2 = Conn.Execute(strSQL)

Response.Write "Entries: <br>" 
do while not rs2.eof
    Response.Write rs2("testavg") & "<br>"
rs2.MoveNext()
loop

strSQL = "SELECT AVG(testavg) AS mittelwert FROM tbl_ergebnisse WHERE testavg >0"
set rs = Conn.Execute(strSQL)

if not rs.eof then
    Response.Write "Mittelwert: " & rs("mittelwert")
else
    Response.Write "EOF" 
end if

这给了我一个奇怪的结果: 查询1返回&#34; 4&#34; (计数是正确的)。查询2不返回任何结果,Query3也不返回结果

表&#34; tbl_ergebnisse&#34;看起来像这样

testavg (DECIMAL)

带条目:3; 3; 4; 5;

表中数据的截图可以在这里找到:www.trinews.at/data.png

enter image description here

为什么我得到一个空记录集的任何建议?直接在数据库上运行查询会返回正确的值。

2 个答案:

答案 0 :(得分:0)

我使用字段类型Decimal(18,0)重新创建了SQL Server Express的问题,并且它按预期工作。

尝试使用CDbl将结果转换为双精度,如下所示。

strSQL = "SELECT testavg FROM tbl_ergebnisse WHERE testavg >0" 
set rs2 = Conn.Execute(strSQL)  
Response.Write "Entries: <br>"  
do while not rs2.eof     
    Response.Write CDbl(rs2("testavg")) & "<br>" 
rs2.MoveNext() 
loop  



strSQL = "SELECT AVG(testavg) AS mittelwert FROM tbl_ergebnisse WHERE testavg >0"
set rs = Conn.Execute(strSQL)  
if not rs.eof then     
    Response.Write "Mittelwert: " & CDbl(rs("mittelwert"))
else     
    Response.Write "EOF"  
end if 

答案 1 :(得分:0)

尝试在MySQL语句中使用CAST将十进制类型转换为ASP ADODB可以理解的数字,否则ASP无法识别MySQL的结果并认为它是EOF。例如:

SELECT CAST(SUM(Entry_Data_1) as UNSIGNED) as score FROM contests_entries