概述:
文本文件是一种非常常用的文件格式,因其通用的阅读方式而经常被用做不同单位的数据交换文件格式。
文本文件需要能够正确导入数据库,
必须要满足以下两个条件其中之一:
1:文本文件的结构格式固定,长度固定.
2:文本文件每行长度不固定,但每个字段之间有特殊符号分开.
本文给出的是满足条件1的文本文件导入到数据库的解决方法,对于满足条件2的文本文件,道理类似,
有兴趣的朋友可以自行测试,如有问题,欢迎交流和沟通!
文本文件是一种非常常用的文件格式,因其通用的阅读方式而经常被用做不同单位的数据交换文件格式。
文本文件需要能够正确导入数据库,
必须要满足以下两个条件其中之一:
1:文本文件的结构格式固定,长度固定.
2:文本文件每行长度不固定,但每个字段之间有特殊符号分开.
本文给出的是满足条件1的文本文件导入到数据库的解决方法,对于满足条件2的文本文件,道理类似,
有兴趣的朋友可以自行测试,如有问题,欢迎交流和沟通!
一:问题描述:
这是老婆大人单位的一个项目,需求其实还是比较清晰的:要求编写Asp.Net程序,自动读取每天生成的文本文件,
根据业务逻辑处理后,保存到SQLSERVER数据库。
文本格式定义:
1:每一行具有相同固定格式,一行就对应于数据库中的一条记录,记录的每个字段都有明确的长度定义。
2:每一行文本都是连续的,中间没有任何的特殊的符号来区分不同的字段。
3:文本中有英文字母和数字,也可能有中文汉字。
4:格式规定,一个英文字母或数字对应的长度为1,中文汉字对应的长度为2.
根据项目需求分析,整个需求并不复杂,一般来说,文本文件需要能够正确导入数据库,
必须要满足以下两个条件其中之一:
1:文本文件的结构格式固定,长度固定.
2:文本文件每行长度不固定,但每个字段之间有特殊符号分开.
根据业务逻辑处理后,保存到SQLSERVER数据库。
文本格式定义:
1:每一行具有相同固定格式,一行就对应于数据库中的一条记录,记录的每个字段都有明确的长度定义。
2:每一行文本都是连续的,中间没有任何的特殊的符号来区分不同的字段。
3:文本中有英文字母和数字,也可能有中文汉字。
4:格式规定,一个英文字母或数字对应的长度为1,中文汉字对应的长度为2.
根据项目需求分析,整个需求并不复杂,一般来说,文本文件需要能够正确导入数据库,
必须要满足以下两个条件其中之一:
1:文本文件的结构格式固定,长度固定.
2:文本文件每行长度不固定,但每个字段之间有特殊符号分开.
上述需求满足条件1,肯定可以能正确导入到数据库。
二:初步的解决思路和步骤
1:读取文本文件内容到数据流StreamReader
2:开始事务
3:循环从数据流中读取每一行到字符串,按表结构定义通过SubString函数对字符串进行截取到每一个字段变量。
4:定义SQL存储过程,传入步骤3中的每个字段值,在存储过程中完成每条记录的插入!
5:执行事务
6:事务回滚
7:错误消息捕捉,返回友好信息提示
8:释放相关资源
1:读取文本文件内容到数据流StreamReader
2:开始事务
3:循环从数据流中读取每一行到字符串,按表结构定义通过SubString函数对字符串进行截取到每一个字段变量。
4:定义SQL存储过程,传入步骤3中的每个字段值,在存储过程中完成每条记录的插入!
5:执行事务
6:事务回滚
7:错误消息捕捉,返回友好信息提示
8:释放相关资源
三:实际开发中出现的问题与解决方法
问题:
实际的程序中,按照文本文件定义的字段长度进行字符串截取,取出来的字符串长度与实际长度不一致,不论是使用bg2312编码还是默认编码。 具体现象通过调试跟踪,在C#中,不论是英文字母还是汉字,取出来的长度也是1,而不是如文本文件中规定的那样,汉字长度为2.导致整个字符串与实际定义的长度不一致,所以在用SubString()函数截取字符串时,无法根据数据结构定义中的长度正确取得每个字段值。
原因分析:
问题原因很明显是编码格式出现问题,汉字保存在磁盘中的编码有多种,常见的有:GB、BIG5 、Unicode、UTF-7、UTF-8等。在文本文件读取过程中, 文件的编码(Encoding)和StreamReader/Writer指定的Encoding不对应,就会出现乱码问题。虽然StreamReader可以根据文本文件格式自动识别编码格 式,正确读取不同编码格式的文本文件。但对于字符串来说,是无法识别字符串中的中文字符与英文,数字的区别,统一都作用长度为1处理。正确的处 理方法是使用将字符串转换为字节数组,对字节数组按文本文件定义的字段长度读取每个字段值。
问题:
实际的程序中,按照文本文件定义的字段长度进行字符串截取,取出来的字符串长度与实际长度不一致,不论是使用bg2312编码还是默认编码。 具体现象通过调试跟踪,在C#中,不论是英文字母还是汉字,取出来的长度也是1,而不是如文本文件中规定的那样,汉字长度为2.导致整个字符串与实际定义的长度不一致,所以在用SubString()函数截取字符串时,无法根据数据结构定义中的长度正确取得每个字段值。
原因分析:
问题原因很明显是编码格式出现问题,汉字保存在磁盘中的编码有多种,常见的有:GB、BIG5 、Unicode、UTF-7、UTF-8等。在文本文件读取过程中, 文件的编码(Encoding)和StreamReader/Writer指定的Encoding不对应,就会出现乱码问题。虽然StreamReader可以根据文本文件格式自动识别编码格 式,正确读取不同编码格式的文本文件。但对于字符串来说,是无法识别字符串中的中文字符与英文,数字的区别,统一都作用长度为1处理。正确的处 理方法是使用将字符串转换为字节数组,对字节数组按文本文件定义的字段长度读取每个字段值。
四:修正的解决思路
1:读取文本文件内容到数据流StreamReader
2:开始事务
3:循环从数据流中读取每一行到字符串,并转换为字节数组。按表结构定义对字节数组进行截取到每一个字段变量。
4:定义SQL存储过程,传入步骤3中的每个字段值,在存储过程中完成每条记录的插入!
5:执行事务
6:事务回滚
7:错误消息捕捉,返回友好信息提示
8:释放相关资源
2:开始事务
3:循环从数据流中读取每一行到字符串,并转换为字节数组。按表结构定义对字节数组进行截取到每一个字段变量。
4:定义SQL存储过程,传入步骤3中的每个字段值,在存储过程中完成每条记录的插入!
5:执行事务
6:事务回滚
7:错误消息捕捉,返回友好信息提示
8:释放相关资源
五:相关的文本文件处理函数参考,
完整的源码见附件
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
因有不少朋友通过留言,希望提供DataAccess类源码,现提供源码,见链接:
转载于:https://blog.51cto.com/alligator/102446