public void ExcelToDataSet()
{
try
{
OpenFileDialog openfiledialog = new OpenFileDialog();
openfiledialog.Filter = "Execl files (*.xlsx)|*.xlsx";
openfiledialog.FilterIndex = 0;
openfiledialog.RestoreDirectory = true;
openfiledialog.Title = "导入文件的路径";
//openfiledialog.ShowDialog();
//if (openfiledialog.FileName == "")
//{
// return;
//}
if (openfiledialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
savePath = System.Convert.ToString(openfiledialog.FileName);
string strCon = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + savePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strCon);
if (conn.State.ToString() == "Open")
{
conn.Close();
}
conn.Open();
string s = conn.State.ToString();
OleDbDataAdapter mycommand = null;
ds = null;
string strExcel = "SELECT * FROM [Sheet1$]";
mycommand = new OleDbDataAdapter(strExcel, conn);
ds = new DataSet();
mycommand.Fill(ds);
conn.Close();
MySqlConnection myconn = new MySqlConnection(ManCode.mysql.mysqlStr);
myconn.Open();
DataRow dr = null;
int C_Count = ds.Tables[0].Columns.Count;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dr = ds.Tables[0].Rows[i];
insertTosql(dr, C_Count, myconn);
}
myconn.Close();
if (ds.Tables[0].Rows.Count < 1)
{
MessageBox.Show("没有数据!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected void insertTosql(DataRow dr, int column_count, MySqlConnection conn)
{
string qrcodeinfo = dr[0].ToString();
string model = dr[1].ToString();
string mysql = "insert into qrcode(qrcodeinfo,model,scanningtime) values('" + qrcodeinfo + "','" + model + "',now())";
MySqlCommand mysqlcmd = new MySqlCommand(mysql, conn);
int res = mysqlcmd.ExecuteNonQuery();
if (res >= 0)
MessageBox.Show("导入成功!");
}
报的错误:
Fatal error encountered during command execution.
麻烦各位大神帮忙解答。