我想在静态方法中使用文本框中的文本,该方法在我的SQL数据库中显示一列。我怎样才能做到这一点?如果我执行下面的代码,我有这个错误:
非静态方法需要对象引用。
这是我的静态方法:
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();
}
}
谢谢!
答案 0 :(得分:3)
您正在静态方法中引用实例成员。你可以:
return Cmd1.ExecuteScalar().ToString();
并在调用methdo Action<string>
代理static
修饰符)由于你的方法看起来像某种事件处理程序,我不会把它变成静态的。
摘要:您必须删除对非静态成员(文本框)的访问权限或使您的方法保持非静态。