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