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; } } } } <img src="http://hi.csdn.net/attachment/201101/15/0_1295110690g8Nx.gif" mce_src="http://hi.csdn.net/attachment/201101/15/0_1295110690g8Nx.gif" alt="" width="422" height="279" /></b>