完整代码如下,比照数据库中的手机号码格式,然后检测出归属地。
复制代码 代码示例:
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;
}
}
}
}
效果图,如下所示: