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 void Btninput_click (object sender, EventArgs e)//Import button click event {folderbrowserdialog dlg = new FolderBrowser Dialog (); Select the Directory dialog box if (dlg. ShowDialog ()! = DialogResult.OK)//Determine if the dot is ok {return; } string path = Dlg. SelectedPath; Gets the directory path of string Str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; Gets the string using (SqlConnection conn = new SqlConnection (str))//using the variable Str for connection {Conn. Open (); using (SqlCommand cmd = conn. CreateCommand ()) {cmd.commandtext = "delete from T_phone"; Delete Table cmd first. ExecuteNonQuery (); }}//Search all. txt files under the specified directory string[] files = directory.getfiles (path, "*.txt", searchoption.alldirectories); using (SqlConnection conn = new SqlConnection (STR)) {Conn. Open (); using (SqlCommand cmd = conn. CreateCommand ()) {cmd.commandtext = "insert INTO T_phone (Starno,endno,name) VALUES (@SN, @EN, @NA) "; foreach (string file in files) {string operator = 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 (string line in lines) {string[] str = LINE.S Plit ('-'); Split string startn = Str[0] by the "-" character; String endn = Str[1]; String Name = Str[2]; Cmd. Parameters.clear (); Clear Parameters cmd first. Parameters.Add (New SqlParameter ("SN", Startn)); Cmd. Parameters.Add (New SqlParameter ("EN", Endn)); Cmd. Parameters.Add (New SqlParameter ("NA", Carrier + Name); Cmd. ExecuteNonQuery (); }} MessageBox.Show ("Import done! "); }}} private void Btncha_click (object sender, EventArgs e)//Query button, click event {string Str = Configurationmanager.c Onnectionstrings["ConnStr"]. ConnectionString; using (SqlConnection conn = new SqlConnection (STR)) {Conn. Open (); using (SqlCommand cmd = conn. CreateCommand ()) {cmd. CommandText = "Select Name from T_phone where starno< @Num and endno> @Num"; Cmd. Parameters.Add (New SqlParameter ("@Num", TxtNum.Text.Trim ())); SqlDataReader dr = cmd. ExecuteReader (); if (Dr. Read ()) {string result =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)