黑马程序员—我自己编写的手机号码导入

---------------------- 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培训、期待与您交流! ----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值