asp.net 中手机号码归属地查询实现代码

作者:袖梨 2022-06-25

第一步:在“引用”项里添加一个引用:System.configuration。

第二步:在项目里右键->添加->新建项->添加一个“应用程序配置文件”即App.config文件。

该文件的代码如下所示:

 代码如下 复制代码



 
   
 

第三步:在Program.cs页面的Main()方法中添加如下连接ADO.Net中创建数据库的代码:

其作用:这段代码时用于读取AD.Net下创建的数据库,不加则读取根目录Debug下的数据库

 代码如下 复制代码

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

第四步:在项目里右键->添加->新建项->添加一个“基于服务的数据库”(数据库名为:Phone.mdb);数据库有七个字段:Id(标识符),StartNo(开始号码),EndNo(结束号码),Sheng(号码所在省、自治区或直辖市),Shi(号码所在市),TMobile(运营商),CardType(卡的类型)。

最后,在窗体Form1.cs页面中添加两个using命令:using System.IO;和using System.Data.SqlClient;然后,在窗体上拖出一个按钮:用于单击时导入数据。
该页面的完整代码如下:

 代码如下 复制代码

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 手机号码归属地
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
 
         private void btnImport_Click(object sender, EventArgs e)
         {
             if (ofdImport.ShowDialog() != DialogResult.OK)
             {
                 return;
             }
                 //打开现有文件以进行读取
 //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"))
                             {
                                 //创建连接非常耗时,因此不要每次操作都创建连接
                                 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)//先在()中给line赋值,然后再进行比较
                                     {
                                         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];
                                         //参数不能重复添加,在while中一直用的就是同一个SqlCommand对象
                                         cmd.Parameters.Clear();//每次添加参数前先清除原先上次执行的参数
                                         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();//假如不清空原先数据,在此会出现SqlException异常(变量名'@Name'已声明)
                                     }
                                 }
                             }
                         }
                 }
                 MessageBox.Show("数据导入成功!");
         }
     }
 }

相关文章

精彩推荐