我想在显示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'似乎是一个实例成员。从你的方法中删除静态。 –