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());
            }
        }
    }
    

      

  • 相关阅读:
    接口和抽象的区别
    接口
    jquery Ajax提交表单数据
    SQL 检查 是否存在 表 临时表
    ASP.NET MVC 设置Area中 Controller 的方法 默认启动页
    json 序列化为数组
    C# Lamda中类似于SQL 中的 In 功能
    各种webservice调用地址
    ASP.NET获取客户端IP地址
    C#反射机制 Type类型
  • 原文地址:https://www.cnblogs.com/hp-discuz/p/5130549.html
Copyright © 2011-2022 走看看