首页 > 其他 > 详细

简易数据导入小工具

时间:2014-03-07 19:45:50      阅读:472      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣

bubuko.com,布布扣
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;

namespace ImportedDataTool
{
    public partial class ImportedDataIntoDBFromTXT : Form
    {
        public ImportedDataIntoDBFromTXT()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件|*.txt|所有文件|*.*";
            of.FilterIndex = 1;
            if (of.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = of.FileName.Trim();
            }
        }
        
        /// <summary>
        /// 开始导入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool isFirst = true;//表头信息标识
                DataTable dt = new DataTable();
                string path = txtPath.Text.Trim();//txt路劲
                string conn = txtStrConn.Text.Trim();//连接字符串
                SqlConnection con = new SqlConnection(conn);
                con.Open();
                string tableName = txtTableName2.Text.Trim();//接收数据的表
                StreamReader smRead = new StreamReader(path, System.Text.Encoding.Default);
                string line;
                while ((line = smRead.ReadLine()) != null)
                {
                    string[] arrStr = line.Split(new char[] { \t });
                    // 如果第一次读取数据则读取的是表头信息
                    if (isFirst)
                    {
                        //录入表头信息到临时table中
                        foreach (var item in arrStr)
                        {
                            dt.Columns.Add(item.ToString().Trim());
                        }
                        isFirst = false;
                    }
                    else
                    {
                        //录入内容信息
                        DataRow dr = dt.NewRow();
                        for (int i = 0; i < arrStr.Count(); i++)
                        {
                            dr[i] = arrStr[i];
                        }
                        dt.Rows.Add(dr);
                    }
                }

                SqlBulkCopy sbc = new SqlBulkCopy(conn, SqlBulkCopyOptions.UseInternalTransaction);
                sbc.DestinationTableName = tableName;
                sbc.BulkCopyTimeout = 180;
                sbc.WriteToServer(dt);
                sbc.Close();
                con.Close();
                MessageBox.Show("数据导入成功", "数据导入成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据导入失败", "数据导入失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



    }
}
bubuko.com,布布扣

简易数据导入小工具,布布扣,bubuko.com

简易数据导入小工具

原文:http://www.cnblogs.com/Moonlight-Shadow/p/3586770.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!