我有一个使用 MySQL .Net 连接的应用程序,但由于某种原因,我在解析结果以返回它们时遇到问题,
public NameValueCollection[] query(string query)
{
connect();
NameValueCollection[] resultSet;
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = query;
connection.Open();
Reader = command.ExecuteReader();
string theserows = "";
while (Reader.Read())
{
for (int i = 0; i < Reader.FieldCount; i++){
theserows += Reader.GetName(i)+"="+Reader.GetValue(i).ToString() + ",";
Count = i;
}
theserows += "\n";
}
connection.Close();
string[] results = theserows.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
int countResultRows = 0;
resultSet = new NameValueCollection[Count];
foreach (string s in results)
{
resultSet[countResultRows] = new NameValueCollection();
string[] result = s.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
foreach (string col in results)
{
if (string.IsNullOrEmpty(col))
continue;
string[] kv = col.Split('=');
resultSet[countResultRows].Add(kv[0], kv[1]);
}
countResultRows++;
}
return resultSet;
}
在此theserows = "site_id=1,\n"
,但我有一个string[] result = s.Split(',');
例外IndexOutOfRangeException
任何人都可以深入了解为什么会发生此错误吗?
另一方面,我正在阅读所有内容然后构建 NameValueCollection 的原因是我想添加一个记录完整查询及其结果的日志系统
编辑::
“count”(小写)更改为 countResultRows
调用栈
HTML5Streamer.exe!HTML5Streamer.Classes.MySQL.query(string query) Line 53 C#
HTML5Streamer.exe!HTML5Streamer.Classes.Query.getSiteId(string domain) Line 17 + 0x10 bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Query.checkUserPass(string username, string password, string domain) Line 31 + 0xb bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Service.ProccessAdminRequest(System.Net.HttpListenerContext context) Line 239 + 0xd9 bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Service.ProcessRequest(System.Net.HttpListenerContext context) Line 176 + 0xb bytes C#
HTML5Streamer.exe!HTML5Streamer.Classes.Service.ListenerCallback(System.IAsyncResult result) Line 150 + 0xb bytes C#
http://i.stack.imgur.com/EST4w.png
第 52 行是resultSet[countResultRows] = new NameValueCollection();
函数开始于第 26 行,结束于 65