使用的方式是 MySqlBulkLoader

方法如下:

1. 转化datatable 为文件

2. 使用MySqlBulkLoader 进行数据的加载

代码:

public static void CreateCSVfile(DataTable dtable, string strFilePath)
{
    StreamWriter sw = new StreamWriter(strFilePath, false);
    int icolcount = dtable.Columns.Count;
    foreach (DataRow drow in dtable.Rows)
    {
    for (int i = 0; i < icolcount; i++)
    {
        if (!Convert.IsDBNull(drow[i]))
        {
        sw.Write(drow[i].ToString());
        }
        if (i < icolcount - 1)
        {
        sw.Write(",");
        }
    }
    sw.Write(sw.NewLine);
    }
    sw.Close();
    sw.Dispose();
}
 
private void ImportMySQL()
{
    DataTable orderDetail = new DataTable("ItemDetail");
    DataColumn c = new DataColumn();        // always
    orderDetail.Columns.Add(new DataColumn("ID", Type.GetType("System.Int32")));
    orderDetail.Columns.Add(new DataColumn("value", Type.GetType("System.Int32")));
    orderDetail.Columns.Add(new DataColumn("length", Type.GetType("System.Int32")));
    orderDetail.Columns.Add(new DataColumn("breadth", Type.GetType("System.Int32")));
    orderDetail.Columns.Add(new DataColumn("total", Type.GetType("System.Decimal")));
    orderDetail.Columns["total"].Expression = "value/(length*breadth)";
 
    //Adding dummy entries
    DataRow dr = orderDetail.NewRow();
    dr["ID"] = 1;
    dr["value"] = 50;
    dr["length"] = 5;
    dr["breadth"] = 8;
    orderDetail.Rows.Add(dr);
 
    dr = orderDetail.NewRow();
    dr["ID"] = 2;
    dr["value"] = 60;
    dr["length"] = 15;
    dr["breadth"] = 18;
    orderDetail.Rows.Add(dr);
    //Adding dummy entries
 
    string connectMySQL = "Server=localhost;Database=test;Uid=username;Pwd=password;";
    string strFile = "/TempFolder/MySQL" + DateTime.Now.Ticks.ToString() + ".csv";
 
    //Create directory if not exist... Make sure directory has required rights..
    if (!Directory.Exists(Server.MapPath("~/TempFolder/")))
    Directory.CreateDirectory(Server.MapPath("~/TempFolder/"));
 
    //If file does not exist then create it and right data into it..
    if (!File.Exists(Server.MapPath(strFile)))
    {
    FileStream fs = new FileStream(Server.MapPath(strFile), FileMode.Create, FileAccess.Write);
    fs.Close();
    fs.Dispose();
    }
 
    //Generate csv file from where data read
    CreateCSVfile(orderDetail, Server.MapPath(strFile));
    using (MySqlConnection cn1 = new MySqlConnection(connectMySQL))
    {
    cn1.Open();
    MySqlBulkLoader bcp1 = new MySqlBulkLoader(cn1);
    bcp1.TableName = "productorder"; //Create ProductOrder table into MYSQL database...
    bcp1.FieldTerminator = ",";
 
    bcp1.LineTerminator = "\r\n";
    bcp1.FileName = Server.MapPath(strFile);
    bcp1.NumberOfLinesToSkip = 0;
    bcp1.Load();
 
    //Once data write into db then delete file..
    try
    {
        File.Delete(Server.MapPath(strFile));
    }
    catch (Exception ex)
    {
        string str = ex.Message;
    }
    }
}

Mysql 批量插入数据的方法的更多相关文章

  1. python使用MySQLdb向mySQL批量插入数据的方法

    该功能通过调用mySQLdb python库中的 cursor.executemany()函数完成批量处理. 今天用这个函数完成了批量插入 例程: def test_insertDB(options) ...

  2. sqlserver 下三种批量插入数据的方法

    本文将介绍三种批量插入数据的方法,需要的朋友可以参考下 本文将介绍三种批量插入数据的方法.第一种方法是使用循环语句逐个将数据项插入到数据库中:第二种方法使用的是SqlBulkCopy,使您可以用其他源 ...

  3. MySQL批量插入数据的几种方法

    最近公司要求测试数据库的性能,就上网查了一些批量插入数据的代码,发现有好几种不同的用法,插入同样数据的耗时也有区别 别的先不说,先上一段代码与君共享 方法一: package com.bigdata; ...

  4. mysql批量插入数据的基类

    自己设计的一个mysql数据库批量添加数据的基类.用于批量向mysql数据库添加数据,子类实现起来很简单,自测性能也还不错. 1.基类实现-BatchAddBase using System.Coll ...

  5. mysql 批量插入数据过多的解决方法

    使用场景: 测试时需要插入100w的数据,跑sql脚本插入非常慢. 存储过程如下: //DELIMITER DROP PROCEDURE if EXISTS createAmountCount; cr ...

  6. mysql 批量插入数据

    MySQL使用INSERT插入多条记录,应该如何操作呢?下面就为您详细介绍MySQL使用INSERT插入多条记录的实现方法,供您参考. 看到这个标题也许大家会问,这有什么好说的,调用多次INSERT语 ...

  7. mysql批量插入数据优化

    一.问题 很早以前做了一个更新功能,就是将A表中的数据全部查找出来,相对B表中改变的数据更新B表,B表中没有的数据插入B表. 最近发现该功能执行速率减慢,有时还跑超时.原来是A表中数据渐渐变多,就有了 ...

  8. SQL Server 批量插入数据的方法

    运行下面的脚本,建立测试数据库和表. --Create DataBase create database BulkTestDB; go use BulkTestDB; go --Create Tabl ...

  9. 在 SQL 中 快速 批量 插入数据的方法

    方法1:逐条执行,速度慢. INSERT INTO testimport (name, message) VALUES ('testname', 'jfksdfkdsfjksadljfkdsfjsdl ...

随机推荐

  1. PHP开发之thinkPHP分层设计

    thinkphp模型层Model.Logic.Service讲解        ThinkPHP支持模型的分层 ,除了Model层之外,我们可以项目的需要设计和创建其他的模型层. 通常情况下,不同的分 ...

  2. Python笔记 #15# Pandas: Missing Data

    10 Minutes to pandas import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = ...

  3. Java学习笔记之MyEclipse 2017 CI 7、CI 8、CI 9和CI 10的安装与激活

    0x00 前言 本文介绍MyEclipse 2017 CI 7.CI 8.CI 9和CI 10的安装与激活. 重要提示:此方法理论上应该能激活MyEclipse 2017 CI所有系列,即激活方法是通 ...

  4. 使用awk分割字符串并且获取分割后的最后一个字符串

    示例:从字符串"you-me-he"中获取he echo "you-me-he" |awk -F '[-]' '{print $NF}'

  5. UVa 10635 王子和公主(LCS转LIS)

    https://vjudge.net/problem/UVA-10635 题意: 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均 ...

  6. Linux的硬链接和软链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link), 也就是软链接.默认情况下,ln命令产生硬链接. [硬连接]硬连 ...

  7. [osg][原创]osg多屏幕显示,会出现透明需要设置的问题

    同事由于新加了一个屏幕,本来用 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); viewer->s ...

  8. jq 抖动效果

    1 .html <div style="margin:50px auto;width:900px;overflow:visible;"> <div id=&quo ...

  9. linux 查看日志最后几行

    tail -n 50 wx.log 示例:查看/var/log/boot.log,只显示最后一行.则执行 tail -n 1  /var/log/boot.log tail -n 1000:显示最后1 ...

  10. Idea使用(摘抄至java后端技术公众号-孤独烟)

    1. idea自动编译需要手动开启: 2. 手动去掉idea自动提示时候不区分字母大小写 3. idea自动导入包 4. 悬浮开关提示:鼠标放上去就给出提示 5. 打开的所有类tabs换行显示,不单行 ...