简易数据导入小工具

简易数据导入小工具_第1张图片

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();
        }

        /// 
        /// 选择文件
        /// 
        /// 
        /// 
        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();
            }
        }
        
        /// 
        /// 开始导入数据
        /// 
        /// 
        /// 
        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);
            }
        }



    }
}

 

转载于:https://www.cnblogs.com/Moonlight-Shadow/p/3586770.html

你可能感兴趣的:(简易数据导入小工具)