---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
通过学习老师讲解的数据的导入导出,我自己跟着老师的方法编写了手机号码的导入,虽然任然存在很多大问题,不过自己花费了不少时间编写,而且也从中学到不少东西,蛮有成就感的,就记录一下。另外这段代码存在极大问题,虽然成功导入了不过不建议参考,这只是我自己的一个记录,请大家不要借鉴。
程序代码:
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.IO;
using System.Data.SqlClient;
namespace 手机号码归属地查询
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnImport_Click(object sender, EventArgs e)
{
if (ofdImport.ShowDialog() != DialogResult.OK)
{
return;
}
foreach (string fileNames in Directory.GetFiles(@"C:\Users\王连\Desktop\手机号码归属地"))
{
if (fileNames.EndsWith(".txt"))
{
string[] place = fileNames.Split('\\','.');
string strPlace = place[5];
File.OpenText(fileNames);
using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream, Encoding.Default))
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDBFilename=|DataDirectory|\Phone.mdf;integrated Security=true;User instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_Numbers(StrNumber,EndNumber,Name) values(@StrNumber,@EndNumber,@Name)";
string line = null;
while ((line = streamReader.ReadLine()) != null)
{
string[] str = line.Split('-');
string strNumber = str[0];
string endNumber = str[1];
string name = strPlace + str[2];
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("StrNumber", strNumber));
cmd.Parameters.Add(new SqlParameter("EndNumber", endNumber));
cmd.Parameters.Add(new SqlParameter("Name", name));
cmd.ExecuteNonQuery();
}
}
}
}
}
}
else
{
return;
}
}
}
}
}
导入后:
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------