Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do you use a CSV as a Linq Database File in (C# Console Application) like a Windows Forms Application?

The command line application will execute various commands depending on the information in the CSV file.

Attempting to process over 60 columns as strongly typed data.


*** Context Notes ***
1) In order to maintain a current application, available resources and abide by restrictions, I am using Visual Studio 2010 to create this command console application.
2) MVC is not available and a graphical interface is too resource intensive.
Posted
Updated 8-Sep-14 11:56am
v2
Comments
kbrandwijk 8-Sep-14 17:54pm    
Can you clarify your question? What is a Linq Database File, and what does 'like a Windows Forms Application' mean? Do you have any existing code? What are you trying to achieve? Reading data from CSV and displaying it on screen? Performing CRUD operations on data from a CSV file in a console application?
Michael Slavik 8-Sep-14 18:07pm    
In Windows Forms Application, the Wizard generates methods to update, query and delete a CSV file. In other words, Linq uses a CSV file as a Database File and Windows Forms Application automates the process for creating the Connection to the CSV files.
In my online searches, results indicate linq can be used to query CSV files.
However, I have not found an equalent for a Command Console application. When I try to adapt the Windows Forms Application Forms & DataSet Designer.cs to use as a Console Application, it appears the Connection uses a Windows Forms specific class.
Not only do I have to determine if a give set of results are true, the determining values and results must also be conveyed.

1 solution

Your first challenge would be to read the CSV into a format that you can easily use. You have a number of options, these two are the most common:

1. Create a class that corresponds to your CSV record. Write code to read the CSV and fill the objects manually, like this:
C#
from line In File.ReadAllLines("filename.csv") 
let parts = line.Split(',') 
select new MyRecord { Field1 = parts[0], Field2 = parts[1] ........ }


2. Create a typed dataset and use the Microsoft Text Driver to read the file using an ODBC collection (stone-age style). You fill the DataSet like you would with any other real database connection.

You can do a lot of intelligent things to make this step nicer, but basically it's a one time thing for one CSV with just 60 columns, so I would go for option 1. Once you have read you CSV into a collection of your objects, you can use anything you would normally use to work with a simple in-memory collection.
 
Share this answer
 
Comments
Michael Slavik 9-Sep-14 11:32am    
Thanks for your prompt response. Before my question was posted, I had been considering both options. I will continue investigating both options with a bias of option 1 after your Solution 1 post. Thanks again.
kbrandwijk 10-Sep-14 16:11pm    
If this solution fits your needs, please mark it as accepted solution to your question. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900