我已經搜索了好幾個小時了,正在尋找解決這個問題的方法。我正在嘗試使用下面的代碼和正確的查詢從我的數據庫中獲取信息。我知道這些查詢的工作原理,因爲我已經在MySQL工作臺中測試了它們。我不斷收到錯誤:在調用read之前無效訪問字段()
Invalid attempt to access field before calling read()
正如你所看到的,我不調用read(),和我的研究纔拿出沒有使用正確的數據庫,我也證明是正確的了。關於爲什麼我收到此錯誤的任何想法?先謝謝您的幫助。
public static string ExecuteSelect(string query)
{
//Example query is: SELECT entity_id FROM catalog_product_flat_1 WHERE sku='itemSku';
string statement = "";
MySqlCommand myCommand = new MySqlCommand(query, _conn);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
myReader.Read();
statement = myReader.GetString(0);
myReader.Close();
return statement;
}
你檢查的'讀的返回值()'它是否返回true(表明有結果被讀取)或false(表示沒有)。 – CodingGorilla