usingMicrosoft.Win32;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Data.SqlClient;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.Shapes;namespacewenjianduqu{/// <summary> ///the interactive logic of Haomaguishudi.xaml/// </summary> Public Partial classHaomaguishudi:window { PublicHaomaguishudi () {InitializeComponent (); } Private voidBtnimport_click (Objectsender, RoutedEventArgs e) {OpenFileDialog Ofg=NewOpenFileDialog (); Ofg. Filter="text Files |*.txt"; if(Ofg. ShowDialog () = =false) { return; }
Put the data in the TXT file into this collection of lines IEnumerable<string> lines =File.readlines (Ofg. FileName, Encoding.default);
You can also use foreach, but the first row of data is not the same as the one behind it, or for a convenient point for(inti =1; I < lines. Count (); i++) {
Effect equivalent to Lines[i]stringline =lines. ElementAt (i);
Split the data in a delimiter and save it in an arraystring[] Stri = line. Split ('\ t'); stringHaoduan = stri[0]; stringDiqu = stri[1];
Raw data information with "" needs to be used. Trim method to remove the double quotes Diqu.trim ('"'); stringleixing = stri[2]; Leixing.trim ('"'); stringQuhao = stri[3]; Quhao.trim ('"');
If the string is preceded by a @ words can be multiple lines of output, or all squeezed into a row too ugly sqlhelper.executenonquery (@"INSERT INTO T_tel (Haoduan,diqu,leixing,quhao) VALUES (@haoduan, @diqu, @leixing, @quhao)", NewSqlParameter ("@haoduan", Haoduan),NewSqlParameter ("@diqu", Diqu),NewSqlParameter ("@leixing", leixing),NewSqlParameter ("@quhao", Quhao)); } MessageBox.Show ("Import succeeded"); } }}
Novice, can only be written like this, optimize what is not a bit, now to upload data speed is very slow, you have to study to see if you can add a progress bar or something
Self-study. NET (4) Upload mobile number attribution to database