[英]c# An object reference is required for the non-static method
我想在静态方法中使用文本框中的文本,该方法在我的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();
}
}
谢谢!
您正在静态方法中引用实例成员。 你可以:
return Cmd1.ExecuteScalar().ToString();
并在调用methdo中设置文本框文本 Action<string>
委托 static
修饰符) 由于您的方法看起来像某种事件处理程序,我不会让它静态。
简介:您必须删除对非静态成员(文本框)的访问权限或使您的方法非静态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.