固定长度记录并创建文件
我必须使用 C# 创建一个固定长度的记录文件。我从数据库中读取记录,然后在一些业务逻辑之后,我想将一些字段写入文本文件。 我对此进行了研究,有些人建议使用 XML 来定义文件,而其他人则格式化字符串,然后将其写入文件中。
有没有正确的方法来做到这一点?我想保持这个面向对象。
感谢您的任何提示。
I have to create a fix length record file using C#. I read the records from database and then after some business logic I would like to write some of the fields into a text file.
I researched this and some recommend using an XML to define the file, while others format the string and then write that into a file.
Is there a proper way to do this? I would like to keep this object oriented.
Thanks for any hints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 http://www.filehelpers.com/ 将数据导出为固定文件格式。您可能还想查看 http://hibernatingrhinos.com/open-source/rhino-etl< /a> 创建一个为您运行导出的进程。 rhino.etl 包含 FileHelpers 的操作。
take a look at http://www.filehelpers.com/ to export the data to a fixed file format. you may also want to look at http://hibernatingrhinos.com/open-source/rhino-etl to create a process that runs the export for you. rhino.etl includes operations for FileHelpers.
您可以使用 Jet 解析文本文件中的固定长度记录。这是一个不错的概述,应该能够帮助您入门:
http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-fixed-length-files.aspx
You can use Jet to parse fixed length records in a text file. This is a decent overview that should be able to get you started:
http://msmvps.com/blogs/deborahk/archive/2009/08/25/reading-fixed-length-files.aspx
过去,当我这样做时,我将数据作为 Char(Length) 从 SQL 中取出,以便由 SQL 处理填充。这使得 C# 非常容易组合在一起。您可以通过在选择数据时转换或转换数据来实现此目的,或者创建一个与文件格式完全相同的表并在其中插入数据,然后将其拉出到 C# 中。
In the past when i've done this, I pull the data out of SQL as a Char(Length) so that the padding is handled by SQL. This makes the C# pretty easy to put together. You can do this by casting or converting the data when you select it, or by creating a table that is the exact format of your file and inserting the data in there before pulling it out into C#.