app.config
<configuration>
<connectionStrings>
<add name=”Constr” connectionString=”Data Source=./SQLEXPRESS;
AttachDBFilename=D:/My Documents/Documents/Visual Studio 2010/Projects/号码查询/号码查询/Numbers.mdf;
Integrated Security=True;User Instance=True”/>
</connectionStrings>
</configuration>
form1.cs
- 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.Configuration;
- using System.IO;
- namespace 号码查询
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnImport_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog Dialog = new FolderBrowserDialog();//打开文件选择框
- if (Dialog.ShowDialog() != DialogResult.OK)
- {
- return;
- }
- string path = Dialog.SelectedPath;
- string connstr=ConfigurationManager.ConnectionStrings[“Constr”].ConnectionString;
- using (SqlConnection conn = new SqlConnection(connstr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = “Delete from T_Numbers”;
- cmd.ExecuteNonQuery();
- }
- }
- string[] files = Directory.GetFiles(path,“*.txt”,SearchOption.AllDirectories );//筛选文件
- using (SqlConnection conn = new SqlConnection(connstr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = “Insert into T_Numbers(FirstNumber,EndNumber,Callerloc) values(@FirstNo,@EndNO,@Callerloc)”;
- foreach (string file in files)
- {
- string MOBILE = Path.GetFileNameWithoutExtension(file);
- string[] lines = File.ReadAllLines(file,Encoding.Default);
- foreach (string line in lines)
- {
- string[] str = line.Split(‘-‘);
- string FirstNumber = str[0];
- string EndNumber = str[1];
- string City = str[2];
- cmd.Parameters.Clear();
- cmd.Parameters.Add(new SqlParameter(“FirstNo”, FirstNumber));
- cmd.Parameters.Add(new SqlParameter(“EndNo”, EndNumber));
- cmd.Parameters.Add(new SqlParameter(“Callerloc”, MOBILE+City));
- cmd.ExecuteNonQuery();
- }
- }
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string connstr=ConfigurationManager.ConnectionStrings[“Constr”].ConnectionString;
- using (SqlConnection conn = new SqlConnection(connstr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = “select Callerloc from T_Numbers where FirstNumber<=@No and EndNumber>=@No”;
- cmd.Parameters.AddWithValue(“No”, textBox1.Text);
- using (SqlDataReader reader = cmd.ExecuteReader())
- {
- if (reader.Read())
- {
- string name = reader.GetString(reader.GetOrdinal(“Callerloc”));
- MessageBox.Show(name);
- }
- else
- {
- MessageBox.Show(“没有找到该号码归属地!”);
- }
- }
- }
- }
- }
- }
- }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37556.html