关于combobox绑定数据的问题,求救呀!!!
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");
------解决方案--------------------静态的方法才是属于类的,非静态的方法要用类的实例去调用。
两种解决方案:
1.public static void InsertCombobox
2.FormOrerate a=new FormOrerate();
a.InsertCombobox;
------解决方案--------------------FormOperate是类, InsertCombobox如果不是static修士的方法,不能直接通过类名访问,要实例化
FormOrerate  f = new FormOrerate();
f.InsertCombobox(xxxx);
------解决方案--------------------public void InsertCombobox(ComboBox cb,string name,string table)
改成
public static void InsertCombobox(ComboBox cb,string name,string table)
,或者调用时用new FormOperate().InsertCombobox(comboBox1, "ClassNameCN", "MaterialClass");
------解决方案--------------------直接用cb.DataSource綁定不就好了
cb.Items.Add("test")應該不會有錯的
是不是你的read有問題,你寫的
string insert = "select"+ name +"from" +table;
name,table傳什麼值,傳了值若沒空格,不就是有問題嗎?
至少數據庫中應是select xx from t,而不是selectxxfromt