简易数据导入小工具

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[] { '	' });
                    // 如果第一次读取数据则读取的是表头信息
                    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);
            }
        }



    }
}
原文地址:https://www.cnblogs.com/Moonlight-Shadow/p/3586770.html