• C#中大批量导入数据SqlBulkCopy


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Win32;
    using System.IO;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                string connStr = ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;
                //int number=0;
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "文本文件|*.txt";
                if (ofd.ShowDialog() != true)
                {
                    return;
                }
    
                string filename = ofd.FileName;
                string[] lines = File.ReadAllLines(filename, Encoding.Default).ToArray();
                DateTime startTime = DateTime.Now;
    
                DataTable table = new DataTable();
                table.Columns.Add("startTelNum");
                table.Columns.Add("City");
                table.Columns.Add("Type");
                foreach (string line in lines)
                {
                    string[] strs = line.Split('	');
                    string startTelNumber = strs[0];
                    string city = strs[1];
                    city = city.Trim('"');
                    string telType = strs[2];
                    telType = telType.Trim('"');
    
                    DataRow row = table.NewRow();
                    row["startTelNum"] = startTelNumber;
                    row["City"] = city;
                    row["Type"] = telType;
                    table.Rows.Add(row);
                }
    
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connStr))
                {
                    bulkCopy.DestinationTableName = "T_TelNum";
                    bulkCopy.ColumnMappings.Add("startTelNum", "StartTelNumber");
                    bulkCopy.ColumnMappings.Add("City", "TelType");
                    bulkCopy.ColumnMappings.Add("Type", "TelArea");
                    bulkCopy.WriteToServer(table);
                }
                TimeSpan ts = DateTime.Now - startTime;
                MessageBox.Show(ts.ToString());
            }
        }
    }
    

      

  • 相关阅读:
    c++ CPO ADL
    c++ intrusive
    c++边界检查
    C++仿函数
    C++ RefBase
    c++ vector容器的尺寸问题
    关于调用约定
    C++学习之字符串类、容器
    C++异常
    Git常用命令大全,迅速提升你的Git水平
  • 原文地址:https://www.cnblogs.com/hp-discuz/p/5130549.html
Copyright © 2020-2023  润新知