我有一个问题,我试图导入一个excel文件到mysql数据库。它正在工作,但它只导入了1行。
OleDbConnection olconn = new OleDbConnection(conStr);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * From [" + comboBox1.Text + "]", olconn);
dt = new DataTable();
DataSet ds = new DataSet();
myDataAdapter.Fill(ds);
olconn.Close();
dataGridView1.DataSource = dt;
//gridControl1.DataSource = dt;
connExcel.Close();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
MySqlConnection con = new MySqlConnection(connStr);
string query = "Insert into excel(IDExcel, studentnumber, fullname, course, yearandsection) Values('" +
ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "','" + ds.Tables[0].Rows[i][3].ToString() + "','" + ds.Tables[0].Rows[i][4].ToString() + "')";
con.Open();
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}
MessageBox.Show("Done Importing!", "Congratulations!", MessageBoxButtons.OK,MessageBoxIcon.Information);发布于 2017-09-22 01:56:52
dataGridView1.DataSource = dt;嗯,我看到的一件事是,上面的行没有任何意义,因为dt没有链接到数据集……您可以初始化dt或只使用ds.Tables。
您是否还可以在代码中的某处放置一个中断,然后在Debug -> Windows ->即时窗口中执行以下行:?ds.Tables.Rows.Count
只是为了验证你是否真的得到了多条记录
https://stackoverflow.com/questions/46350254
复制相似问题