zoukankan      html  css  js  c++  java
  • 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());
            }
        }
    }
    

      

  • 相关阅读:
    Cocos2dx使用ios内支付IAP具体流程-白白
    Linux下完美使用find+grep实现全局代码搜索
    扩展GCD 中国剩余定理(CRT) 乘法逆元模版
    java实现floyd统计天津地铁的网站距离
    并查集
    HTML5:理解head
    Pivotal Cloud Foundry安全原理解析
    tar、7z(7zip)压缩/解压缩指令的使用
    Linux下C编程的学习_1
    【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)
  • 原文地址:https://www.cnblogs.com/hp-discuz/p/5130549.html
Copyright © 2011-2022 走看看