C # import data in large batches SqlBulkCopy,

Source: Internet
Author: User

C # import data in large batches 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. configurati On; using System. data. sqlClient; using System. data; namespace WpfApplication1 {/// <summary> // MainWindow. interaction logic of xaml // </summary> public partial class MainWindow: Window {public MainWindow () {InitializeComponent ();} private void button#click (object sender, RoutedEventArgs e) {string connStr = ConfigurationManager. connectionStrings ["dbConStr"]. connectionString; // int number = 0; OpenFileDialog Ofd = new OpenFileDialog (); ofd. Filter = "text file | *. 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 ('\ t'); 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 ());}}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.