手机归属地查询的部分代码

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.Data.SqlClient;
using System.IO;
using System.Configuration;
namespace Telephone
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        string strSql = ConfigurationManager.ConnectionStrings["CnnTelephone"].ConnectionString;
        string city = "";
        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (txtTelephone.Text.Length < 7)
            {
                this.textBox1.Text ="你输入的手机号码不正确";
                return;
            }
            string number = txtTelephone.Text;
            string select = "select city from telephone where @number between startnumber and lastnumber";
            try
            {
                using (SqlConnection sqlcnn = new SqlConnection(strSql))
                {
                    SqlCommand sqlcmm = new SqlCommand(select, sqlcnn);
                    sqlcmm.Parameters.AddWithValue("@number", number);
                    sqlcnn.Open();
                    SqlDataReader dr = sqlcmm.ExecuteReader();
                    if (dr.Read())
                    {
                        city = dr["city"].ToString();

                        this.textBox1.Text = "你查询的号码是[" + txtTelephone.Text + "]/r/n";
                        this.textBox1.Text += "归属地是:" + city;
                    }
                    else
                    {
                        this.textBox1.Text = "请查看你输入的手机号码是否正确";
                    }
                    dr.Close();
                    sqlcnn.Close();
                }
            }
            catch (Exception ex)
            {
                this.textBox1.Text = "没有查到"+ex.Message;
            }
        }

        private void txtTelephone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
            }
            if(e.KeyChar =='/b')
            {
                e.Handled = false;
            }

        }
        private void ClearDataBase()
        {
            using (SqlConnection sqlcnn = new SqlConnection(strSql))
            {
                string delete = "truncate table Telephone";
                SqlCommand sqlcmm = new SqlCommand(delete, sqlcnn);
                sqlcnn.Open();
                sqlcmm.ExecuteNonQuery();
                sqlcnn.Close();
                Application.DoEvents();
            }
        }
        private void UpdateDataBase()
        {
            try
            {
                using (SqlConnection sqlcnn = new SqlConnection(strSql))
                {
                    string delete = "update Telephone set city=REPLACE(city,'联通','电信') where SUBSTRING(startNumber,1,3) in(133,153)";
                    SqlCommand sqlcmm = new SqlCommand(delete, sqlcnn);
                    sqlcnn.Open();
                    sqlcmm.ExecuteNonQuery();
                    sqlcnn.Close();
                    Application.DoEvents();
                }
            }
            catch
            {
            }
        }
        private void btnInset_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ClearDataBase();
                progressBar1.Maximum = ofd.FileNames.Length;
                progressBar1.Minimum = 0;
                this.Height += 70;
                SqlConnection sqlcnn = new SqlConnection(strSql);
                string insert = "insert into Telephone(startNumber,lastNumber,city) values(@start,@last,@city)";
                SqlCommand sqlcmm = new SqlCommand(insert, sqlcnn);
                sqlcnn.Open();
                foreach (var item in ofd.FileNames)
                {
                    string shang = Path.GetFileNameWithoutExtension(item);
                    lblNumber.Text = string.Format("正在导入第{0}个文件{1},共有{2}个文件", progressBar1.Value, shang, ofd.FileNames.Length);
                    StreamReader sr = new StreamReader(item, Encoding.Default);
                    //string shang = item.Substring(item.LastIndexOf('//') + 1, item.LastIndexOf('.') - item.LastIndexOf('//') - 1);
                    while (!sr.EndOfStream)
                    {
                        Application.DoEvents();
                        string[] line = sr.ReadLine().Split('-');
                        sqlcmm.Parameters.Clear();
                        sqlcmm.Parameters.AddWithValue("@start", line[0]);
                        sqlcmm.Parameters.AddWithValue("@last", line[1]);
                        sqlcmm.Parameters.AddWithValue("@city", shang + "[" + line[2] + "]");
                        sqlcmm.ExecuteNonQuery();
                    }
                    sr.Close();
                    progressBar1.Value += 1;
                }
                sqlcnn.Close();
                this.Height -= 70;
                UpdateDataBase();
            }
           
        }
   

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值