C # mobile phone number attribution Query

Source: Internet
Author: User
Tags import database

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 ");
}

}
}
}

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.