using DataToDeal; using LumenWorks.Framework.IO.Csv; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; 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; namespace CSVReaderFast { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; } DataTable dt = null; //update VEYear set DescriptionChinese=replace(DescriptionChinese,'?','') public string str = "Data Source=.;Initial Catalog=RBVehicle;User ID=sa;Password=xwl;Max Pool Size = 512"; void MainWindow_Loaded(object sender, RoutedEventArgs e) { //简单对应数据库表名,其实完全可以数据库获取 string[] tableNamelist = { "RBAutoCalc", "RBRegAdjustments", "RBRegions", "RBStdEquip", "RBVehicle", "VEFamily", "VEMake", "VEYear" }; foreach (var item in tableNamelist) { this.cbx1.Items.Add(item); } } //打开csv文件 private void Button_Click_1(object sender, RoutedEventArgs e) { dt = new DataTable(); OpenFileDialog window = new OpenFileDialog(); window.FileName = "文件"; window.DefaultExt = "*.*"; window.Filter = "CSV文件|*.CSV"; Nullable<bool> result = window.ShowDialog(); if (result == true) { string fileName = window.FileName; using (CsvReader csv = new CsvReader(new StreamReader(fileName), true)) { //字段数量 int fieldCount = csv.FieldCount; //标题数组 string[] headers = csv.GetFieldHeaders(); //创建列名称 for (int i = 0; i < headers.Count(); i++) { DataColumn dc = new DataColumn(headers[i]); dt.Columns.Add(dc); } //只进的游标读取 while (csv.ReadNextRecord()) { DataRow dr = dt.NewRow(); for (int j = 0; j < fieldCount; j++) { dr[j] = csv[j]; } dt.Rows.Add(dr); } if (dt.Rows.Count > 0) { this.DataGrid1.ItemsSource = dt.DefaultView; this.labe1.Content = "共" + dt.Rows.Count + "条"; } } } } private void Button_Click(object sender, RoutedEventArgs e) { if (this.cbx1.SelectedIndex != -1) { if (dt != null && dt.Rows.Count > 0) { string TableName = this.cbx1.SelectedItem.ToString(); string returnvalue = new HelpFile().SqlBulkCopyData(TableName, dt, str); if (returnvalue.Contains('@')) { MessageBox.Show("导入异常:" + returnvalue.Split('@').Last()); } MessageBox.Show("导入成功!"); lable1.Content = returnvalue; } else { MessageBox.Show("没有数据可导入,请先加载数据!"); } } } } }