Quickly read CSV flat files and import databases, simple gadgets

Source: Internet
Author: User

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

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.