我收到錯誤: 在調用Read() at之前無效嘗試訪問字段:string result = Reader.GetString(0);在調用Read()之前訪問字段的嘗試無效()
我不完全知道該怎麼做或什麼是錯,雖然
internal int GetCharGuidByName(string charactername, MySqlConnection connection)
{
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "SELECT guid FROM characters WHERE name=\""+charactername+"\";";
// Initialize MySQL Reader
Reader = command.ExecuteReader();
Reader.Read();
string result = Reader.GetString(0);
// If the character doesn't exist or isn't entered, return 0
int charguid = 0;
if (result != String.Empty)
{
charguid = Convert.ToInt32(result);
}
return charguid;
}
你有沒有打開asigning讀者Command.ExecuteReader卻方法實施前的連接?並將Reader.Read()更改爲if(Reader.Read()){...},並且如果沒有要讀取的結果,代碼將不會進入if語句。 – 2012-07-13 19:14:40
是的,連接是打開的,也試圖在這個函數內創建一個連接(不使用函數接收到的連接)並打開它,它也是一樣的 – user1071461 2012-07-13 19:16:18