-3
namespace Gtec_Solution{
    public partial class Main : Form {
        string myconnection = "datasource=localhost;port=3306;username = root;password = 12345V";
        public Main(string userName){

           InitializeComponent();
           load_table_1();
        }

        public void load_table_1(DataTable dt){
           DataTable d;
           Load_Table_Class ld = new Load_Table_Class();
           string Query_1 = "SELECT * FROM gtec_computer.employee";
           d = ld.loading_table(Query_1);
           dataGridView3.DataSource = d;
        }
     }
}

namespace Gtec_Solution{
    public class Load_Table_Class{
        public DataTable loading_table(string Query){
            string myconnection = "datasource=localhost;port=3306;username = root;password = 12345V";
            MySqlConnection con = new MySqlConnection(myconnection);
            MySqlCommand cmd = new MySqlCommand(Query, con);
            con.Open();
            MySqlDataAdapter mda = new MySqlDataAdapter();
            mda.SelectCommand = cmd;
            DataTable dt = new DataTable();
            mda.Fill(dt);
            BindingSource bs = new BindingSource();
            bs.DataSource = dt;
            mda.Update(dt);
            return dt;
       }
   }
}
4

5 回答 5

2

好吧,错误消息说这个调用:

 load_table_1();

无效,因为此方法在没有参数的情况下没有重载。但是dt,您声明的参数load_table_1无论如何都不会使用,因此您可以安全地删除它:

    public void load_table_1()
    {
        DataTable d;
        Load_Table_Class ld = new Load_Table_Class();
        string Query_1 = "SELECT * FROM gtec_computer.employee";
        d = ld.loading_table(Query_1);
        dataGridView3.DataSource = d;
    }
于 2013-06-26T08:23:40.463 回答
0

该方法load_table_1(DataTable dt)似乎不需要或不使用参数DataTable dt。从签名中删除它。

于 2013-06-26T08:22:46.957 回答
0

您需要将 a 传递DataTable给该方法load_table_1

您已将函数定义为:

public void load_table_1(DataTable dt)
    {
        DataTable d;
        Load_Table_Class ld = new Load_Table_Class();
        string Query_1 = "SELECT * FROM gtec_computer.employee";
        d = ld.loading_table(Query_1);
        dataGridView3.DataSource = d;
    }

因此,您需要这样做:

  DataTable dt = new DataTable(); // Or populate it from your service
  load_table_1(dt); // pass the dt object as an argument to load_table_1

或者DataTable从方法中删除参数。例如:

public void load_table_1()
    {
        DataTable d;
        Load_Table_Class ld = new Load_Table_Class();
        string Query_1 = "SELECT * FROM gtec_computer.employee";
        d = ld.loading_table(Query_1);
        dataGridView3.DataSource = d;
    }
于 2013-06-26T08:23:14.087 回答
0

在方法中Main

load_table_1();

但在课堂上的其他地方Main

公共无效load_table_1(数据表dt)

您需要传递 to 的DataTable实例load_table_1

于 2013-06-26T08:23:46.853 回答
0

这里你调用一个没有参数的方法:

public Main(string userName)
{
    InitializeComponent();
    load_table_1();   //THIS LINE

}

然而,您调用的方法需要一个参数:

public void load_table_1(DataTable dt) // IN HERE
{
    DataTable d;
    Load_Table_Class ld = new Load_Table_Class();
    string Query_1 = "SELECT * FROM gtec_computer.employee";
    d = ld.loading_table(Query_1);
    dataGridView3.DataSource = d;
}

第二种方法不使用参数,所以只需将其删除即可:

public void load_table_1() // DELETE PARAM HERE
{
    DataTable d;
    Load_Table_Class ld = new Load_Table_Class();
    string Query_1 = "SELECT * FROM gtec_computer.employee";
    d = ld.loading_table(Query_1);
    dataGridView3.DataSource = d;
}
于 2013-06-26T08:25:58.897 回答