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.
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.
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
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#.
Related
I am looking to import a csv file in a windows form, datagridvew1 and then parse/update some of the columns into datagridview2, and finally write the output as csv
I am struggling to convert the required fields from datagridview1 into datagridview2
Any suggestions welcome.
Thanks
There's a million ways to do this but my suggestion would be assuming you are writing your code for windows use the microsoft text driver to load the data directly from the text file into a dataset (roughly as shown in: CSV upload in .NET using ODBC including the answer's fix for the bug in the posters code). Then clone and dump out the data into the second dataset/csv file as so: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/copying-dataset-contents .
Lastly: I would suggest not blindly looping though the datarows and outputting row[i] + "," etc like a see a lot of posts on similar questions suggesting. The problem is escaping fields that contain commas themselves properly etc. Better to use a library, one I've used in the past and found nice is CsvParser you can get that via nuget. It's better to use libaries anyways because when the next complication comes up like utf-8 encoded text often you just have to pass in an additional parameter rather than learning and properly covering all the edge cases of how to do the encoding yourself.
I'm looking to make my own file format .
that format should contains pictures/pdf/and other files ...
also need to know how I can packer-unpacker for this format to unpack files from it/pack in it & reading the pictures from my own format to picture boxes on my WinForm for example.
I've searched but didn't really found what I'am looking for
I hope someone can help me , thank you
Zip is an excellent choice. Because you can encrypt the file and of course reduce the file size in some cases (text and uncompressed things). But if you want to create your own file format you can easily decide rules for your storage and order inside the file. Then serialize the info into the file. For example by object serialization or by writing the binary date to file object by object .
if you really want to write your own file format then I would suggest one of two things. One, you could do it entirely in binary at which point you would want to do a 'chunk' format. Chunk format is to basically have a header to each subsection. The header contains the size of both the header as well as the size of the payload. Create a serialization class for your header then add the bytes to the filestream from your payload. Actually pretty easy to do.
Second (and easier) way to do this would be to create an XML format. Create a master class for your format then add all of the data as collections of sub classes under that. Once you have that, use any of .net xml serialization classes to serialize it out to disk.
You can also use SQLite for your purposes. It provides dbms power without needing server. That is popular solution for your problem.
System.Data.SQLite is an ADO.NET adapter for SQLite.
I am trying to fetch data from database and export it to .csv file format, which is similar to ETL process. But I want to do this in C#.
SQL Query to fetch data from database.
Format the data to a specified file specification.
Convert it to .csv file.
I know that 1st step is easy to do, I am struggling to find a way for 2nd and 3rd step.
I can't comment on the file specification, as you haven't described it, but writing CSV files is pretty easy. Unlike XML etc the format is so simplistic you can write directly to a StreamWriter using WriteLine. All you need is to output a first line that contains the column names separated with commas, then for each row returned from your SQL Query write the column values in the same order separated by commas. The only real gotcha is escaping, e.g. dealing with commas, quotes, etc by surrounding each value with quotes and escaping any quotes in the value.
The example below does just that for a DataTable:
http://dotnetguts.blogspot.co.nz/2007/01/exporting-datatable-to-csv-file-format.html
I figured out a simple and efficient way to do this:
1.SQL Query to fetch data from database.
2. Format the data to a specified file specification.
For the first 2 steps, I have used the Dapper.NET, which
took care of the database part as well as formatting. Dapper helped to convert the SQL results to a LIST by which I fulfilled the file specifications.
3.Convert it to .csv file.
For converting the SQL results to CSV file, I have used FileHelpers
Library which is more simple than I expected.
Hope it will help someone.
I have a task at hand, read a database that was created with HTML-OS, i assume the format is DB4 or DB5. the task is to open and parse this database in a c# application...it can be ASP.NET or WinForms...bottom line is i need to extract this indexed data. below is a small sample of what it looks like when i open the file with notepad:
dbtype 3.046 = 0 T ¨j
I have some ideas on parsing with possibly using RegEx but i would like to see some ideas or a real way of reading this file would be even better!
Thank you in advance!
It looks like the database format is partially defined on their site:
The HTML/OS Database (PDF)
If possible I'd look at using their HTML/OS language to export it, rather than trying to reverse-engineer the database format.
You could make an htmlos page that would export the data from the database and store this in a csv for example. Then pick up the file from the other machine, after which you could execute something else to remove the csv file.
To answer devHead's question.. yes, HTML/OS accepts http posts.
(Just FYI: If you have any questions, feel free to ask.. I program in html/os.)
I have two TextRanges from two different RichTextBoxes, and four strings from regular textboxes. I would like to save all this information in one file, and then be able to load it later. Whats the best approach?
I've been reading some about it, and it seems that reading all into one memorystream and then save it to a file is one way to do it. And then parse this content later.
Anyone that want to share some experience, and simple code?
For a simple approach consider creating a class with string properties for each of your textbox texts. You could then set the properties when you want to save your text, use XML serialization to save the class to an XML formatted file, and then read it back at a later time.
The advantage of this approach is that you will not need to hande low-level file handling or parsing yourself.
Searching for C# and XML Serialization will yield plenty of code examples.
One solution is already provided by you: save in file and read after.
Another could be, in case if the data is too big, is using http://msdn.microsoft.com/en-us/library/dd997372.aspx Memory Mapped Fies.