1. Import Database
The data of the mobile phone number's home location is saved in a TXT file in a folder, as shown in:
The data format in a TXT file is as follows:
13003000000-13003009999-Hefei
130030316-13003029999-Bengbu
13003030000-13003049999-Wuhu
13003050000-13003069999-Hefei
13003070000-13003079999-Huainan
13003080000-13003089999-Hefei
.........
Create databases and tables, table field ID, startno, endno, and area (area consists of TXT file names and cities)
Scan the TXT file in this folder to import it to the created database table.
The Code is as follows: Of course, an import button and a folderbrowserdialog control are required on winfoorm. Used to select the folder.
If (FBD. showdialog ()! = Dialogresult. OK)
{
Return;
}
String Path = FBD. selectedpath;
String [] files = directory. getfiles (path, "*. txt", searchoption. alldirectories );
Using (sqlconnection conn = new sqlconnection (dbconn ))
{
Conn. open ();
Using (sqlcommand cmd = conn. createcommand ())
{
Cmd. commandtext = "insert into t_cellno (startno, endno, Area) values (@ SnO, @ Eno, @ area )";
String lines = NULL;
Foreach (string file in files)
{
String fname = path. getfilename (File );
String filename = path. getfilenamewithoutextension (File );
Using (filestream = file. openread (File ))
{
Streamreader reader = new streamreader (file, system. Text. encoding. getencoding ("gb2312 "));
While (lines = reader. Readline ())! = NULL)
{
String [] STRs = lines. Split ('-');
String startno = STRs [0]. tostring ();
String endno = STRs [1]. tostring ();
String areas = STRs [2]. tostring ();
String area = filename + areas;
Cmd. Parameters. Clear ();
Cmd. Parameters. Add (New sqlparameter ("Sno", startno ));
Cmd. Parameters. Add (New sqlparameter ("Eno", endno ));
Cmd. Parameters. Add (New sqlparameter ("area", Area ));
Cmd. executenonquery ();
}
}
}
MessageBox. Show ("database imported successfully ");
2. Enter the mobile phone number and click the button to query the home location code.
Using (sqlconnection conn = new sqlconnection (dbconn ))
{
Conn. open ();
Using (sqlcommand cmd = conn. createcommand ())
{
Cmd. commandtext = "select * From t_cellno where startno <= @ No and endno >=@ no ";
Cmd. Parameters. Add (New sqlparameter ("no", textbox1.text ));
Using (sqldatareader reader = cmd. executereader ())
{
If (reader. Read ())
{
String area = reader. getstring (reader. getordinal ("area "));
MessageBox. Show ("mobile phone number" + area );
}
Else
{
MessageBox. Show ("sorry, check this number ");
}
}
}
}