执_念 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 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素