C#文本文件导入数据库_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#文本文件导入数据库

C#文本文件导入数据库

 2014/12/22 13:24:32  大水怪  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem
  • 标签:C# 文件 数据库 数据 导入数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
using System.Data.SqlClient;


namespace 手机号码归属地查询
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter="文本文件|*.txt";
            if(ofd.ShowDialog()!=true)
            {
                return;
            }
            //IEnumerable<string> lines = File.ReadLines(ofd.FileName, Encoding.Default);
            string[] lines = File.ReadLines(ofd.FileName, Encoding.Default).ToArray();
                        for (int i = 1; i < lines.Count();i++ )
            {
                 string line =lines[i];

                string[] segs= line.Split('\t'); //注意文件文本的分割方式
                string startTelNum=segs[0];
                string city = segs[1];
                city = city.Trim('"');
                string teltye= segs[2];
                teltye = teltye.Trim('"');
                SqlHelper.ExcuteNonQuery("insert into T_tel (StartNum ,TelArea,TelType) values ('" + startTelNum + "','" + city + "','" + teltye + "')");
                //SqlHelper.ExcuteNonQuery("insert into T_tel (StartNum ,TelArea,TelType) values (@startnum,@telarea,@teltype)",
                //    new SqlParameter("@startnum", startTelNum),
                //    new SqlParameter("@telarea", city),
                //    new SqlParameter("@teltype", teltye));
            }
            MessageBox.Show("导入成功!");
        }
    }

}

  此方法效率低下,耗时长,仅为学习!

发表评论
用户名: 匿名