我想在顯示SQL數據庫中的列的靜態方法中使用文本框中的文本。我怎樣才能做到這一點?如果我下面執行我的代碼,我有這樣的錯誤:c#非靜態方法需要對象引用
An object reference is required for the non-static method.
這是我的靜態方法:
static void OnTagsReported(ImpinjReader sender, TagReport report)
{
SqlConnection Cn;
SqlCommand Cmd;
//SqlCommand Cmd1;
Cn = new SqlConnection(@"Data Source=DESKTOP- ODCFVR1\SQLEXPRESS;Initial Catalog=RFID;Integrated Security=True");
Cn.Open();
// This event handler is called asynchronously
// when tag reports are available.
// Loop through each tag in the report
// and print the data.
foreach (Tag tag in report)
{
MessageBox.Show ("voici l'antenne :"+ tag.AntennaPortNumber +", EPC :"+ tag.Epc);
Cmd = new SqlCommand ("INSERT INTO EPC (Epc_CODE) values('" + tag.Epc + "')", Cn);
Cmd.ExecuteNonQuery();
SqlCommand Cmd1 = new SqlCommand();
Cmd1.CommandText = " select * from Epc Where EPC_code = '" + tag.Epc + "' ";
Cmd1.Connection = Cn;
String result = " ";
Cmd1.ExecuteNonQuery();
result = Cmd1.ExecuteScalar().ToString();
textBox5.Text = result; // here is my problem
Cn.Close();
}
}
謝謝!
**警告**您的代碼極易遭受SQL注入攻擊。 –
'textBox5'似乎是一個實例成員。從你的方法中刪除靜態。 –