执_念 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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B