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