C#_datatable 写入大量数据_BulkCopy

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DataBaseTest
{
    /// <summary>
    /// InportExportDataTest.xaml 的交互逻辑
    /// </summary>
    public partial class InportExportDataTest : Window
    {
        public InportExportDataTest()
        {
            InitializeComponent();
        }

        private void txtImport_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件|*.txt";
            if (ofd.ShowDialog() != true)
            {
                return;
            }
            string filename = ofd.FileName;
            //小文件可以用ReadLines,大文件用FileStream
            IEnumerable<string> lines =  File.ReadLines(filename,Encoding.Default);

            foreach (string line in lines)
            {
                string[] segs = line.Split('|');
                string name = segs[0];
                int age = Convert.ToInt32(segs[1]);
                SqlHelper.ExecuteNonQuery("insert into T_DataImport values (@name,@age)",
                    new SqlParameter("@name",name),new SqlParameter("@age",age));

            }
            MessageBox.Show("成功导入"+lines.Count()+"条");


            
            
        }

        private void btnBulk_Click(object sender, RoutedEventArgs e)
        {
            string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件|*.txt";
            if (ofd.ShowDialog() != true)
            {
                return;
            }
            string filename = ofd.FileName;
            //小文件可以用ReadLines,大文件用FileStream
            string[] lines = File.ReadLines(filename, Encoding.Default).ToArray();

            DateTime startTime = DateTime.Now;

            DataTable table = new DataTable();
            table.Columns.Add("StartTelNum");
            table.Columns.Add("City");
            table.Columns.Add("TelType");
            for(int i=0;i<lines.Length;i++)
            {
                string line = lines[i];
                string[] strs = line.Split('	'); //TAB键
                string startTelNum = strs[0];
                string city = strs[1];
                city = city.Trim('"');//去掉双引号
                string telType = strs[2];
                telType = telType.Trim('"');

                DataRow row = table.NewRow();//DataRow定义有Internal,所以要new一个row对象
                row["StartTelNum"] = startTelNum;
                row["City"] = city;
                row["TelType"] = telType;
                table.Rows.Add(row);//NewRow只是创建,还没有插入,只是本地的table
            }

            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connStr))
            {
                bulkCopy.DestinationTableName="T_Student"; //目标table
                bulkCopy.ColumnMappings.Add("StartTelNum", "StartTelNum");//本地table与数据库中table column的mapping
                bulkCopy.ColumnMappings.Add("City", "TelArea");
                bulkCopy.ColumnMappings.Add("TelType", "TelType");
                bulkCopy.WriteToServer(table);
            }
            TimeSpan ts = DateTime.Now - startTime;
        }
    }
}


原文地址:https://www.cnblogs.com/MarchThree/p/3720436.html