Implementation code of mobile phone number attribution query in asp.net

Source: Internet
Author: User
Tags connectionstrings

Step 1: Add a reference in "reference": System. configuration.

Step 2: Right-click the project and choose "add"> "new item"> Add an "application configuration file", that is, the App. config file.

The code for this file is as follows:

The code is as follows: Copy code

<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConnectionStrings>
<Add name = "ConnStr" connectionString = "Data Source =. SQLEXPRESS; AttachDBFilename = | DataDirectory | Phone. mdf;
Integrated Security = True; User Instance = True "/>
</ConnectionStrings>
</Configuration>

Step 3: add the following code to connect to ADO. Net in the Main () method on the Program. cs page to create a database:

Function: this code is used to read the database created in AD. Net. If this code is not added, the database in Debug under the root directory is read.

The code is as follows: Copy code

String dataDir = AppDomain. CurrentDomain. BaseDirectory;
If (dataDir. EndsWith (@ "binDebug ")
| DataDir. EndsWith (@ "binRelease "))
            {
DataDir = System. IO. Directory. GetParent (dataDir). Parent. Parent. FullName;
AppDomain. CurrentDomain. SetData ("DataDirectory", dataDir );
            }

Step 4: Right-click the project and choose Add> new item> add a "service-based database" (the database name is Phone. mdb); the database has seven fields: Id (identifier), StartNo (start number), EndNo (end number), Sheng (number of the province, autonomous region, or municipality ), shi, TMobile, and CardType ).

Finally, add two using commands: using System. IO; and using System. Data. SqlClient on the Form1.cs page. Then, drag a button on the form to import Data when you click.
The complete code for this page is as follows:

The code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Using System. Data. SqlClient;
 
Namespace mobile phone number
 {
Public partial class Form1: Form
     {
Public Form1 ()
         {
InitializeComponent ();
         }
 
Private void btnImport_Click (object sender, EventArgs e)
         {
If (ofdImport. ShowDialog ()! = DialogResult. OK)
             {
Return;
             }
// Open an existing file for reading
// Using (FileStream fileStream = new FileStream (ofdImport. FileName, FileMode. Open,
// FileAccess. Read, FileShare. None ))
Using (FileStream fileStream = File. OpenRead (ofdImport. FileName ))
                 {
Using (StreamReader streamReader = new StreamReader (fileStream ))
                     {
Using (SqlConnection conn = new SqlConnection (@ "Data Source =. SQLEXPRESS; AttachDBFilename =" +
AppDomain. CurrentDomain. BaseDirectory + "Phone. mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True "))
                             {
// It takes a long time to create a connection. Therefore, do not create a connection for each operation.
Conn. Open ();
Using (SqlCommand cmd = conn. CreateCommand ())
                                 {
Cmd. commandText = "Insert into T_PhoneNum (StartNo, EndNo, Sheng, Shi, TMobile, CardType) values (@ StartNo, @ EndNo, @ Sheng, @ Shi, @ TMobile, @ CardType) ";
String line = null;
While (line = streamReader. ReadLine ())! = Null) // assign a value to line in () before comparison
                                     {
String [] strs = line. Split ('-');
String startNo = strs [0];
String endNo = strs [1];
String sheng = strs [2];
String shi = strs [3];
String merchant = strs [4];
String type = strs [5];
// Parameters cannot be added repeatedly. The same SqlCommand object is always used in while.
Cmd. Parameters. Clear (); // The Parameters previously executed are cleared before each parameter is added.
Cmd. Parameters. Add (new SqlParameter ("StartNo", startNo ));
Cmd. Parameters. Add (new SqlParameter ("EndNo", endNo ));
Cmd. Parameters. Add (new SqlParameter ("Sheng", sheng ));
Cmd. Parameters. Add (new SqlParameter ("Shi", shi ));
Cmd. Parameters. Add (new SqlParameter ("TMobile", merchant ));
Cmd. Parameters. Add (new SqlParameter ("CardType", type ));
Cmd. ExecuteNonQuery (); // if the original data is not cleared, an SqlException exception occurs here (variable Name '@ name' has been declared)
                                     }
                                 }
                             }
                         }
                 }
MessageBox. Show ("data import successful! ");
         }
     }
 }

Related Article

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.