Using System.IO;
There is a TXT file for the database mobile number, the format is: 13500000000-13560000000-China Mobile
Search Result: Hunan Mobile [Shaoyang]
Folder selection dialog box FolderBrowserDialog;
String[] Directory.GetFiles (path, type, option)//Search for files in directory according to wildcard characters
Path.getfilenamewithoutextension (file name)//Get the file name (do not extend the name)
Path.Combine (path 1, path 2)//merge two paths
Path.getfilename (path)//Get file name (with suffix)
Path.GetFullPath (path)//Get the full path of the file
Purge old data before importing
The interface is like this:
A text input box: txtmessage
A query button: Btncha
An Import button: Btninput
A Label control: labmessage
Private voidBtninput_click (Objectsender, EventArgs e)//Import button click event{FolderBrowserDialog Dlg=NewFolderBrowserDialog ();//Select Directory dialog Box if(DLG. ShowDialog ()! = DialogResult.OK)//Determine If the point is OK { return; } stringPath = dlg. SelectedPath;//Get directory path stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;//Get String using(SqlConnection conn =NewSqlConnection (STR))//connecting using the variable str{Conn. Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext="Delete from T_phone";//Delete Table Firstcmd. ExecuteNonQuery (); } } //search all. txt files in the specified directory string[] files = directory.getfiles (path,"*.txt", searchoption.alldirectories); using(SqlConnection conn =NewSqlConnection (STR)) {Conn. Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext="INSERT INTO T_phone (starno,endno,name) VALUES (@SN, @EN, @NA)"; foreach(stringFileinchfiles) { stringCarrier = path.getfilenamewithoutextension (file);//get file name, no extension string[] lines = File.ReadAllLines (File, Encoding.default);//reads all rows, the default encoding is UTF-8 foreach(stringLineinchlines) { string[] str = line. Split ('-');//split by "-" character stringStartn = str[0]; stringENDN = str[1]; stringName = str[2]; Cmd. Parameters.clear (); //clear the Parameters first .Cmd. Parameters.Add (NewSqlParameter ("SN", Startn)); Cmd. Parameters.Add (NewSqlParameter ("EN", ENDN)); Cmd. Parameters.Add (NewSqlParameter ("NA", Carrier +Name)); Cmd. ExecuteNonQuery (); }} MessageBox.Show ("Import Done! "); } } } Private voidBtncha_click (Objectsender, EventArgs e)//Query button, click event { stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn. Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext="Select Name from T_phone where starno< @Num and endno> @Num"; Cmd. Parameters.Add (NewSqlParameter ("@Num", TxtNum.Text.Trim ())); SqlDataReader Dr=cmd. ExecuteReader (); if(Dr. Read ()) {stringResult =convert.tostring (dr["Name"]); Labmessage.text=NULL; Labmessage.text="Number Address:"+result; } Else{Labmessage.text="Number Address:"+"Address Unknown! "; } } } }
"ADO" 5, mobile phone attribution to query (Winfrom)