执_念 2015-07-22 10:49 采纳率: 0%
浏览 2657

C#ExecuteNonQuery操作mysql数据库报错

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.
            麻烦各位大神帮忙解答。
  • 写回答

3条回答 默认 最新

  • threenewbee 2015-07-22 10:50
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services