消息关闭
    暂无新消息!
  1. 不知道怎样读取.out 文本内容,.out 文本格式如
    ,怎么处理去掉空格,table符,双竖线呢?

  2. 我参照这篇博客,C#从文本文件中读取数据大批量导入数据库

我写的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication5
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            /* string filePath = openFileDialog1.FileName;
             string extension = Path.GetExtension(filePath);
             string conStr, sheetName;

             conStr = string.Empty;
             switch (extension)
             {

             }
             */
           
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文档|*.out";
            //如果用户没有选择文件并确定则直接返回
            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            //获得文件名包括路径
            string fileName = ofd.FileName;

            try
            {
                //定义一个开始时间
                DateTime startTime = DateTime.Now;

                //创建内存临时数据表来存储从文本文件中读取出来的数据
                using (DataTable table = new DataTable())
                {
                    //为数据表创建相对应的数据列
                    table.Columns.Add("流水号");
                    table.Columns.Add("编号");
                    table.Columns.Add("国家");
                    table.Columns.Add("价格");
                    table.Columns.Add("类别");
                    table.Columns.Add("数量");
                    table.Columns.Add("金额");

                    //因为文件比较大,所有使用StreamReader的效率要比使用File.ReadLines高
                    using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
                    {
                        while (!sr.EndOfStream)
                        {
                            DataRow dr = table.NewRow();//创建数据行

                            string readStr = sr.ReadLine();//读取一行数据
                            if (readStr.StartsWith("1"))//去掉标题行
                            {
                                string[] strs = readStr.Split(new char[] { '\t', '"' }, StringSplitOptions.RemoveEmptyEntries);//将读取的字符串按"制表符/t“和””“分割成数组

                                string liushui = strs[0];
                                string bianhao = strs[1];
                                string guojia = strs[2];
                                string jiage = strs[3];
                                string leibie = strs[4];
                                string shuliang = strs[5];
                                string jine = strs[6];

                                //往对应的 行中添加数据
                              /*  dr["startNum"] = startNum;
                                dr["numArea"] = numArea;
                                dr["numType"] = numType;*/
                                dr["流水号"] = liushui;
                                dr["编号"] = bianhao; 
                                dr["价格"] = jiage; 
                                dr["类别"] = leibie; 
                                dr["数量"] = shuliang; 
                                dr["金额"] = jine; 
                                table.Rows.Add(dr);//将创建的数据行添加到table中

                            }
                        }
                    }
                   SqlConnection con = new SqlConnection("Data Source = HENRY; Initial Catalog = students; Integrated Security = True");
                    con.Open();
                   // SqlConnection connStr = null;
                  
                    SqlBulkCopy bulkCopy = new SqlBulkCopy(con);

                    bulkCopy.DestinationTableName = "test";//设置数据库中对象的表名

                    //设置数据表table和数据库中表的列对应关系
                    bulkCopy.ColumnMappings.Add("流水号", "流水号");
                   // bulkCopy.ColumnMappings.Add("numArea", "FNumArea");
                    bulkCopy.ColumnMappings.Add("编号", "编号");
                    bulkCopy.ColumnMappings.Add("价格", "价格");
                    bulkCopy.ColumnMappings.Add("类别", "类别");
                    bulkCopy.ColumnMappings.Add("数量", "数量");
                    bulkCopy.ColumnMappings.Add("金额", "金额");
                    bulkCopy.WriteToServer(table);//将数据表table复制到数据库中

                    TimeSpan ts = DateTime.Now - startTime;
                    MessageBox.Show("共导入" + table.Rows.Count + "条数据,总共花费时间:" + ts.ToString());
                }

            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }
    }
}

截图:


1个回答

︿ 1

我没看你连接的那篇文章,你可以有两种处理方法:
1.C#读取一行数据时,用||将每行数据拆分然后trim掉空格,用insert into插入到表
2.将每一行读到SQL的表中,然后用SQL的功能拆分为多列,插入到目标表

你的语句这句有问题,你的文件的分隔不不是制表符(t)也不是双引号,而是||,你要改下
string[] strs = readStr.Split(new char[] { 't', '"' }, StringSplitOptions.RemoveEmptyEntries);//将读取的字符串按"制表符/t“和””“分割成数组

用||拆分,然后用trim去掉空格,再用replace函数去掉不可见的特殊字符如t