usingdatatodeal;usingLUMENWORKS.FRAMEWORK.IO.CSV;usingMicrosoft.Win32;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespacecsvreaderfast{/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); This. Loaded + =mainwindow_loaded; } DataTable DT=NULL; //Update veyear Set Descriptionchinese=replace (Descriptionchinese, '? ', ') Public stringstr ="Data Source=.;i Nitial catalog=rbvehicle; User Id=sa; PASSWORD=XWL; Max Pool Size ="; voidMainwindow_loaded (Objectsender, RoutedEventArgs e) { //simple corresponding database table name, in fact, can be fully database access string[] Tablenamelist = {"Rbautocalc","rbregadjustments","rbregions","Rbstdequip","rbvehicle","vefamily","Vemake","Veyear" }; foreach(varIteminchtablenamelist) { This. Cbx1. Items.Add (item); } } //Open CSV file Private voidButton_click_1 (Objectsender, RoutedEventArgs e) {DT=NewDataTable (); OpenFileDialog window=NewOpenFileDialog (); Window. FileName="file"; Window. DefaultExt="*.*"; Window. Filter="csv file |*. CSV"; Nullable<BOOL> result =window. ShowDialog (); if(Result = =true) { stringFileName =window. FileName; using(Csvreader csv =NewCsvreader (NewStreamReader (FileName),true)) { //Number of fields intFieldCount =CSV. FieldCount; //Header Array string[] headers =CSV. Getfieldheaders (); //Create a column name for(inti =0; I < headers. Count (); i++) {DataColumn DC=NewDataColumn (Headers[i]); Dt. Columns.Add (DC); } //forward-only cursor reads while(CSV. Readnextrecord ()) {DataRow Dr=dt. NewRow (); for(intj =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 ="Total"+ dt. Rows.Count +"Strip"; } } } } Private voidButton_Click (Objectsender, RoutedEventArgs e) { if( This. cbx1. SelectedIndex! =-1) { if(dt! =NULL&& dt. Rows.Count >0) { stringTableName = This. Cbx1. Selecteditem.tostring (); stringReturnValue =NewHelpFile (). Sqlbulkcopydata (TableName, DT, str); if(ReturnValue. Contains ('@') {MessageBox.Show ("Import Exception:"+ returnvalue. Split ('@'). Last ()); } MessageBox.Show ("Import Successful! "); Lable1. Content=returnvalue; } Else{MessageBox.Show ("no data to import, please load the data first! "); } } } }}
Quickly read CSV flat files and import databases, simple gadgets