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.Data.SqlClient;
using System.IO;
using System.Configuration;
namespace Telephone
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string strSql = ConfigurationManager.ConnectionStrings["CnnTelephone"].ConnectionString;
string city = "";
private void btnSelect_Click(object sender, EventArgs e)
{
if (txtTelephone.Text.Length < 7)
{
this.textBox1.Text ="你输入的手机号码不正确";
return;
}
string number = txtTelephone.Text;
string select = "select city from telephone where @number between startnumber and lastnumber";
try
{
using (SqlConnection sqlcnn = new SqlConnection(strSql))
{
SqlCommand sqlcmm = new SqlCommand(select, sqlcnn);
sqlcmm.Parameters.AddWithValue("@number", number);
sqlcnn.Open();
SqlDataReader dr = sqlcmm.ExecuteReader();
if (dr.Read())
{
city = dr["city"].ToString();
this.textBox1.Text = "你查询的号码是[" + txtTelephone.Text + "]/r/n";
this.textBox1.Text += "归属地是:" + city;
}
else
{
this.textBox1.Text = "请查看你输入的手机号码是否正确";
}
dr.Close();
sqlcnn.Close();
}
}
catch (Exception ex)
{
this.textBox1.Text = "没有查到"+ex.Message;
}
}
private void txtTelephone_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
if(e.KeyChar =='/b')
{
e.Handled = false;
}
}
private void ClearDataBase()
{
using (SqlConnection sqlcnn = new SqlConnection(strSql))
{
string delete = "truncate table Telephone";
SqlCommand sqlcmm = new SqlCommand(delete, sqlcnn);
sqlcnn.Open();
sqlcmm.ExecuteNonQuery();
sqlcnn.Close();
Application.DoEvents();
}
}
private void UpdateDataBase()
{
try
{
using (SqlConnection sqlcnn = new SqlConnection(strSql))
{
string delete = "update Telephone set city=REPLACE(city,'联通','电信') where SUBSTRING(startNumber,1,3) in(133,153)";
SqlCommand sqlcmm = new SqlCommand(delete, sqlcnn);
sqlcnn.Open();
sqlcmm.ExecuteNonQuery();
sqlcnn.Close();
Application.DoEvents();
}
}
catch
{
}
}
private void btnInset_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
ClearDataBase();
progressBar1.Maximum = ofd.FileNames.Length;
progressBar1.Minimum = 0;
this.Height += 70;
SqlConnection sqlcnn = new SqlConnection(strSql);
string insert = "insert into Telephone(startNumber,lastNumber,city) values(@start,@last,@city)";
SqlCommand sqlcmm = new SqlCommand(insert, sqlcnn);
sqlcnn.Open();
foreach (var item in ofd.FileNames)
{
string shang = Path.GetFileNameWithoutExtension(item);
lblNumber.Text = string.Format("正在导入第{0}个文件{1},共有{2}个文件", progressBar1.Value, shang, ofd.FileNames.Length);
StreamReader sr = new StreamReader(item, Encoding.Default);
//string shang = item.Substring(item.LastIndexOf('//') + 1, item.LastIndexOf('.') - item.LastIndexOf('//') - 1);
while (!sr.EndOfStream)
{
Application.DoEvents();
string[] line = sr.ReadLine().Split('-');
sqlcmm.Parameters.Clear();
sqlcmm.Parameters.AddWithValue("@start", line[0]);
sqlcmm.Parameters.AddWithValue("@last", line[1]);
sqlcmm.Parameters.AddWithValue("@city", shang + "[" + line[2] + "]");
sqlcmm.ExecuteNonQuery();
}
sr.Close();
progressBar1.Value += 1;
}
sqlcnn.Close();
this.Height -= 70;
UpdateDataBase();
}
}
}
}