c#實現手機號碼歸屬地查詢

用C#來實現手機號碼的歸屬地查詢,在ASP.NET中很容易實現,可以調用其他網頁進行鏈接,而在WINFORM下面可以採用兩種方式來實現,一種是用WEB控件來顯示該網頁,和天氣預報查詢實現原理一樣,一種就是採用本地數據庫的方式來實現,關於本地數據庫實現的原理網絡上面很多,就是截取號碼段與數據庫中相應字段值比較,Select 出對應的信息。先發張效果圖吧。

全部源代碼如下:    需要下載的話點擊這裏:手機號碼歸屬地查詢(C#源碼)

 

C#手機號碼歸屬地查詢源碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Text.RegularExpressions;
using System.IO;

namespace 手機號碼查詢
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
string phoneNum = "";//存儲完整手機號碼
        string tel = "";//存儲手機號碼的前綴

        
private void tsbtnLocal_Click(object sender, EventArgs e)
        
{
            
if (!File.Exists("data"))//檢查數據庫是否存在
            {
                MessageBox.Show(
"數據庫文件丟失!請找回!""消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
return;
            }

            phoneNum 
= tstxtTel.Text.Trim();//完整手機號碼
            tel = phoneNum.Substring(03);//取手機號碼前面三位判斷
            if (checkTel())
            
{
                phoneNum 
= phoneNum.Substring(07);//取手機號碼前面七位進行查詢
                string sql = "select * from '" + tel + "' where numberrange='" + phoneNum + "' limit 0,1";
                SQLiteConnection con 
= null;
                SQLiteCommand cmd 
= null;
                SQLiteDataReader dr 
= null;
                
try
                
{
                    con 
= new SQLiteConnection("Data Source="+Application .StartupPath +"\\data");
                    cmd 
= new SQLiteCommand(sql, con);
                    con.Open();
                    dr 
= cmd.ExecuteReader();
                    
if (dr.HasRows)//如果存在此記錄
                    {
                        txtLocaltion.Text 
= dr.GetValue(1).ToString();
                        txtType.Text 
= dr.GetValue(2).ToString();
                        txtZipCode.Text 
= dr.GetValue(3).ToString();
                        txtCityNum.Text 
= dr.GetValue(4).ToString();
                    }

                    
else
                    
{
                        MessageBox.Show(
"暫時未查找到此號碼的記錄!""消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                                     
                }

                
catch (Exception ex)
                
{
                    MessageBox.Show(
"暫時未查找到此號碼的記錄!"+ex.Message , "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                
finally
                
{
                    dr.Close();
                    dr.Dispose();
                    con.Close();
                    con.Dispose();
                    tstxtTel.Focus();
                }

            }

        }

        
//檢驗輸入手機號碼的合法性和正確性
        private bool checkTel()
        
{           
            
if (phoneNum == null)//如果號碼爲空
            {
                MessageBox.Show(
"號碼不能爲空!""消息", MessageBoxButtons.OK, MessageBoxIcon.Information );
                tstxtTel.Focus();
                
return false;
            }

            Match num 
= Regex.Match(phoneNum , "[0-9]+");
            
if (!num.Success)//如果號碼不爲數字
            {
                MessageBox.Show(
"手機號碼輸入錯誤!""消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tstxtTel.Focus();
                
return false;
            }

            
if (phoneNum.Length < 7)//如果號碼長度小於七位
            {
                MessageBox.Show(
"手機號碼必須要大於7位!""消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tstxtTel.Focus();
                
return false;
            }

            
if (phoneNum.Length > 11)//如果號碼長度大於十一位
            {
                MessageBox.Show(
"手機號碼必須要小於11位!""消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tstxtTel.Focus();
                
return false;
            }
            
            
if (tel != "130" && tel != "131" && tel != "132" && tel != "133" && tel != "134" &&
                tel 
!= "135" && tel != "136" && tel != "137" && tel != "138" && tel != "139" &&
                tel 
!= "150" && tel != "151" && tel != "152" && tel != "153" && tel != "156" &&
                tel 
!= "158" && tel != "159")//如果不是手機號碼
            {
                MessageBox.Show(
"手機號碼輸入錯誤""消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
return false;
            }

            
return true;
        }


        
private void tstxtTel_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
// 特殊鍵, 不處理
            if ((int)e.KeyChar <= 31)
            
{
                
return;
            }


            
// 非數字鍵, 放棄該輸入
            if (!char.IsDigit(e.KeyChar))
            
{
                e.Handled 
= true;
                
return;
            }

        }


        
private void tsbtnInternet_Click(object sender, EventArgs e)
        
{
            phoneNum 
= tstxtTel.Text.Trim();
            
string url = @"http://api.showji.com/Locating/query.aspx?m=" + phoneNum ;
            wbMain.Navigate(url);
        }


        
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        
{
            MessageBox.Show(
"作者:劉典武 (游龍工作室)" + "\r\n" + "網址:http://www.liudianwu1988.51.com""呵呵", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Application.ExitThread();
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{
            
//tstxtTel.Focus();//這樣初始化焦點不行
        }


        
private void Form1_Shown(object sender, EventArgs e)
        
{
            tstxtTel.Focus();
//這樣才正確
        }

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章