我使用MySQL的.Net連接,但由於某種原因,我遇到解析結果返回他們問題的應用程序,IndexOutOfRangeException已投入串.split C#
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是我想補充一個記錄系統,它記錄了完整的查詢和它的業績
編輯::
「計數」(小寫)改爲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行是在65
IndexOutOfRangeException必須在'的resultSet拋出[數] =新的NameValueCollection();' – Leri
嘗試使用'串[]結果= theserows.Split( '\ n',StringSplitOptions.RemoveEmptyEntries); '和'string [] result = s.Split(',',StringSplitOptions.RemoveEmptyEntries)'並告訴我們會發生什麼...... – Marco
這是你的真實代碼嗎?名爲'count'和'Count'的變量是一個非常糟糕的主意。 –