public class FormOperate
{
public void InsertCombobox(ComboBox cb,string name,string table)
{
cb.Items.Clear();//清空ComBox
string insert = "select"+ name +"from" +table;
SqlConnection conn = new SqlConnection("server =127.0.0.1;uid = sa; pwd =allen;database =MaterialDataBase");
SqlCommand cmd = new SqlCommand(insert, conn);//构造连接字符串并切打开数据库连接
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
cb.Items.Add(read[0].ToString());//循环读取数据
}
read.Close();// 关闭数据集
conn.Close();//关闭数据库连接
}
}调用FormOperate.InsertCombobox总是报错“非静态的字段、方法或属性“MaterialParametersManagement.FormOperate.InsertCombobox(System.Windows.Forms.ComboBox, string, string)”要求对象引用 E:\SJTU\MaterialManagementSystem\MaterialParametersManagement\MaterialParametersManagement\Forms\ParameterBro.cs 43 13 MaterialParametersManagement”
调用语句如下:
FormOperate.InsertCombobox(comboBox1, "ClassNameCN", "MaterialClass");
{
public void InsertCombobox(ComboBox cb,string name,string table)
{
cb.Items.Clear();//清空ComBox
string insert = "select"+ name +"from" +table;
SqlConnection conn = new SqlConnection("server =127.0.0.1;uid = sa; pwd =allen;database =MaterialDataBase");
SqlCommand cmd = new SqlCommand(insert, conn);//构造连接字符串并切打开数据库连接
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
cb.Items.Add(read[0].ToString());//循环读取数据
}
read.Close();// 关闭数据集
conn.Close();//关闭数据库连接
}
}调用FormOperate.InsertCombobox总是报错“非静态的字段、方法或属性“MaterialParametersManagement.FormOperate.InsertCombobox(System.Windows.Forms.ComboBox, string, string)”要求对象引用 E:\SJTU\MaterialManagementSystem\MaterialParametersManagement\MaterialParametersManagement\Forms\ParameterBro.cs 43 13 MaterialParametersManagement”
调用语句如下:
FormOperate.InsertCombobox(comboBox1, "ClassNameCN", "MaterialClass");
解决方案 »
- 想在2个独立的.net程序间传递数据,请教用什么办法速度较快
- 很奇怪的问题,RegistryKey修改注册表,值总是为a
- 事件与速度问题
- winform 有时候在datagridview 的checkbox里面设置readonly=false无效果,要窗口最小化下在最大化下就有效果,代码如何控制?
- mschart上随着鼠标的移动出现十字的形状
- 找。。FJGoodGood(_FJ_强中强)
- 数据格式的小问题
- 如何用C#实现将Web页面中的内容转存到Word文档中?
- C# DES加密问题.
- 用C#写的组件,在ASP中怎么调用法呢?
- 关于源代码加密的问题,不知道有没有什么好办法,期待大神解答
- 求大神帮忙解决日期控件问题
两种解决方案:
1.public static void InsertCombobox
2.FormOrerate a=new FormOrerate();
a.InsertCombobox;
f.InsertCombobox(xxxx);
改成
public static void InsertCombobox(ComboBox cb,string name,string table)
,或者调用时用new FormOperate().InsertCombobox(comboBox1, "ClassNameCN", "MaterialClass");
cb.Items.Add("test")應該不會有錯的是不是你的read有問題,你寫的
string insert = "select"+ name +"from" +table;
name,table傳什麼值,傳了值若沒空格,不就是有問題嗎?
至少數據庫中應是select xx from t,而不是selectxxfromt