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.
麻烦各位大神帮忙解答。
http://www.cnblogs.com/tigerjacky/archive/2010/09/03/1816991.html
string mysql = "insert into qrcode(qrcodeinfo,model,scanningtime) values('" + qrcodeinfo + "','" + model + "',now())";
打印一下你的sql语句,然后到数据库中执行一下,可能是语法错误
查询MYSQL 数据库 时常出现 错误.错误信息 如下:Fatal error encountered during command execution.
出现这种问题有可能是两种情况
1.查询结果包含海量数据。长时间检索无响应。这种情况,需要优化SQL,尽量减少数据输出量。
2.表结构损坏。可以通过 CHECK TABLE REPAIR TABLE 语句进行检测或修复。
附 CHECK TABLE 语法说明
CHECK TABLE tbl_name[,tbl_name] ... [option] ...
option= {QUICK | FAST | MEDIUM | EXTENDED | CHANGED}检查一个或多个表是否有错误。CHECK TABLE对MyISAM和InnoDB表有作用。对于MyISAM表,关键字统计数据被更新。CHECK TABLE也可以检查视图是否有错误,比如在视图定义中被引用的表已不存在。