我的应用程序是从XLSX电子表格中获取大量数据(400k +记录),在表单上的DataGridView中显示每个工作表中的数据,还允许您将选定工作表的数据导出到mySQL中。
导出将在它自己的线程上发生(请注意,这里有很多内容尚未完成):
private AddItemCallBack AddItemDelegate = new AddItemCallBack(AddItemMethod);
private delegate void AddItemCallBack(int Total);
private void lnkExport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
sName = ddlTables.SelectedValue.ToString();
pb = progressBar1;
var t = new Thread(() => ExportData(sName));
t.Start();
}
private void ExportData(string SheetName = "")
{
string sql = "select * from " + String.Format(tablename, SheetName);
OleDbCommand cmd = new OleDbCommand(sql, conn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
connStr = "Data Source=localhost; Initial Catalog=test; User ID=root; Password=Ly@12157114";
MySqlConnection con = new MySqlConnection(connStr);
totalRecords = ds.Tables[0].Rows.Count;
currentRecords = 0;
foreach (DataRow row in ds.Tables[0].Rows)
{
sql = "insert into planet(clubid, clubname, acctno, title, firstname, lastname, cell, email, derp, accttype) " +
"values(@id, @name, @acct, @title, @fname, @lname, @cell, @email, @derp, @type)";
MySqlCommand command = new MySqlCommand(sql, con);
command.Parameters.AddWithValue("@id", row[0]);
command.Parameters.AddWithValue("@name", row[1]);
command.Parameters.AddWithValue("@acct", row[2]);
command.Parameters.AddWithValue("@title", row[3]);
command.Parameters.AddWithValue("@fname", row[4]);
command.Parameters.AddWithValue("@lname", row[5]);
command.Parameters.AddWithValue("@cell", row[6]);
command.Parameters.AddWithValue("@email", row[7]);
command.Parameters.AddWithValue("@derp", row[8]);
command.Parameters.AddWithValue("@type", row[9]);
try
{
con.Open();
command.ExecuteNonQuery();
con.Close();
currentRecords = currentRecords + 1;
this.Invoke(this.AddItemDelegate, new object[] { totalRecords });
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private static void AddItemMethod(int Total)
{
progressBar1.Maximum = Total;
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value = progressBar1.Value + 1;
}
else
{
progressBar1.Value = 0;
}
}
VS抱怨
错误1非静态字段,方法或属性'ImportExcel.Form1.progressBar1'需要对象引用G:\ ImportExcel \ ImportExcel \ Form1.cs 139 13 ImportExcel
对于AddItemMethod void中的progressBar1控件引用的每个实例。
我可以从void中删除静态类型定义,但是我在定义CallBack的行上得到了同样的错误,说AddItemMethod不是静态的。
真的不知道该怎么办。有什么建议吗?
答案 0 :(得分:6)
使您的AddItemMethod非静态并使用构造函数初始化您的回调。
private AddItemCallBack AddItemDelegate;
private delegate void AddItemCallBack(int Total);
private void AddItemMethod(int Total)
{
progressBar1.Maximum = Total;
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value = progressBar1.Value + 1;
}
else
{
progressBar1.Value = 0;
}
}
public Form1()
{
InitializeComponent();
AddItemDelegate = new AddItemCallBack(AddItemMethod);
}
答案 1 :(得分:0)
progressBar1
是实例字段,因此您无法通过static
方法访问它。您需要将其设为static
字段或将AddItemMethod
更改为实例方法。
答案 2 :(得分:0)
AddItemMethod
非静态。AddItemCallback
设置为实例方法,因为字段初始值设定项仅在构造函数之前执行,因此可能不依赖于非静态成员。这就是编译器在您将AddItemMethod
更改为实例方法时抱怨的原因。在构造函数中初始化该字段以规避它。private AddItemCallBack AddItemDelegate;
public Form1()
{
AddItemDelegate = AddItemMethod;
// ... other stuff
InitializeComponents(); // totally guessed, but seems like Form1 is auto generated
}
答案 3 :(得分:-1)
最简单的方法就是将progressBar1作为参数传递给静态方法AddItemMethod