c# winfrom 手机号码归属地查询的简单例子(图文)

发布时间:2019-08-15编辑:脚本学堂
为大家介绍一个c# winform实现的用于手机号码归属地查询的例子,较之前我们介绍过的查询手机号码归属的例子,这个相对简单些,更适合新手朋友参考。

完整代码如下,比照数据库中的手机号码格式,然后检测出归属地。
 

复制代码 代码示例:
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.Configuration;
using System.Data.SqlClient;
namespace 号码归属地查询
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
           string conn=ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
           string number,num;
           number = textBox1.Text.Trim();
           num = number.Substring(0,7);
           using (SqlConnection sqlconn = new SqlConnection(conn))
           {
               sqlconn.Open();
               SqlCommand comm = sqlconn.CreateCommand();
               comm.CommandText = "select * from list where num='"+num+"'";
               SqlDataReader read = comm.ExecuteReader();
               if (!read.Read())
               {
                   MessageBox.Show("没有您要找的号码的归属地!");
               }
               else
               {
                   lb_City.Text = read.GetString(read.GetOrdinal("city"));
                   lb_num.Text = read.GetString(read.GetOrdinal("code"));
                   lb_stype.Text = read.GetString(read.GetOrdinal("cardtype"));
               }
           }
        }
        private void textBox1_KeyPress(object sender,KeyPressEventArgs e)
        {
            if (e.KeyChar<='z'&&e.KeyChar>='a')
            {
                e.Handled =true;
            }
            if (textBox1.Text.Length == 11)
            {
                textBox1.ForeColor = System.Drawing.Color.Red;
            }
        }
       
    }
}

效果图,如下所示:
手机号码归属查询