当前位置: 编程技术>.net/c#/asp.net
c# winfrom 手机号码归属地查询的简单例子(图文)
来源: 互联网 发布时间:2014-08-30
本文导语: 完整代码如下,比照数据库中的手机号码格式,然后检测出归属地。 代码示例: 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;
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='a')
{
e.Handled =true;
}
if (textBox1.Text.Length == 11)
{
textBox1.ForeColor = System.Drawing.Color.Red;
}
}
}
}
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='a')
{
e.Handled =true;
}
if (textBox1.Text.Length == 11)
{
textBox1.ForeColor = System.Drawing.Color.Red;
}
}
}
}
效果图,如下所示: