我收到此錯誤,我不知道爲什麼。調用返回字符串c的方法時出錯#
「的對象引用需要非靜態字段,方法或 財產」
爲什麼我要在這裏有一個對象引用?我的代碼如下:
public string GetChanges()
{
string changelog = "";
MySqlConnection connection = new MySqlConnection("server=127.0.0.1;uid=root;pwd=pass;database=data");
try
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("SELECT `change_log` FROM version WHERE ID = '1'", connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
changelog = reader.GetString(0);
}
}
connection.Close();
}
catch
{
//MessageBox.Show(e.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return changelog;
}
我打電話,像這樣上面的函數:
string changelog = GetChanges();
爲什麼在這種情況下需要一個對象引用?我無法使用靜態,因爲我正在創建一個不能用靜態方法工作的Web服務。我怎樣才能改變這個使用對象?
感謝
你究竟在哪裏調用'getChanges()'? – Rhumborl
您的'GetChanges'方法是一個實例方法_by default_。您不能在沒有所屬類型名稱的情況下調用它。將它設爲'static'或者調用它的類型如'string changelog = MyType.GetChanges();' –
你在哪裏調用'GetChanges'方法?這聽起來應該將方法的邏輯從Web服務接口分離出來,然後調用邏輯代替代碼中的服務。調用Web服務方法直接聞起來 - 你基本上試圖欺騙接口。 – Luaan