在使用ASP連接MySQL數據庫獲取大量數據時,可能會遇到以下報錯信息:
[MySQL][ODBC 5.3(a) Driver][mysqld-8.0.27]Commands out of sync; you can't run this command now
這是因為在獲取MySQL數據時,ASP會將數據存儲在緩存中,當數據量過大時,緩存會被占滿,從而導致該錯誤。
解決該問題有如下兩種方法:
- 增加緩存大小
- 使用多個連接獲取數據
Set conn = Server.CreateObject("ADODB.Connection") conn.ConnectionString = "DRIVER={MySQL ODBC 5.3 Driver}; SERVER=localhost; DATABASE=mydb; UID=myuser; PASSWORD=mypassword; OPTION=3" conn.Open conn.Execute "SET SESSION query_cache_size=268435456;"
通過修改MySQL查詢緩存大小,可以增加緩存的空間,從而減少該錯誤出現的概率。
Function GetData() Dim conn, cmd, rs Set conn = Server.CreateObject("ADODB.Connection") conn.ConnectionString = "DRIVER={MySQL ODBC 5.3 Driver}; SERVER=localhost; DATABASE=mydb; UID=myuser; PASSWORD=mypassword; OPTION=3" conn.Open Set cmd = Server.CreateObject("ADODB.Command") cmd.ActiveConnection = conn cmd.CommandText = "SELECT * FROM mytable" Set rs = Server.CreateObject("ADODB.Recordset") rs.CursorLocation = adUseServer rs.Open cmd, , adOpenForwardOnly, adLockReadOnly Do While Not rs.EOF '處理數據 rs.MoveNext Loop rs.Close Set rs = Nothing Set cmd = Nothing conn.Close Set conn = Nothing End Function
使用多個連接可以將數據分批獲取,從而減少緩存被占滿的情況出現。
下一篇mysql monty