Windows Develop Bookmark and Share   
 index > Windows Forms General > Can I query an embedded (csv file) resource with LINQ?
 

Can I query an embedded (csv file) resource with LINQ?

I'm trying to query an embedded resource file (csv text) with LINQ, but nothing I've tried has worked. Does anybody know how? My failed attempts are listed below:

1)

OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM Application.Properties.Resources.PresidentialLifeSpansCSV", cn);

<-- At runtime, I get the err msg:"The Microsoft Jet database engine could not find the object 'Application.Properties.Resources.PresidentialLifeSpansCSV'. Make sure the object exists and that you spell its name and the path name correctly."


2) string[] csvText = Properties.Resources.PresidentialLifeSpansCSV.Split('\n'); //<- same err msg as above

3)

TextFile f = Properties.Resources.PresidentialLifeSpansCSV; <--I tried File and TextFile. Textfile gives me "The type or namespace name 'TextFile' could not be found (are you missing a using directive or an assembly reference?)" and File gives me "Cannot declare a variable of static type 'System.IO.File'"


OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM csvText", cn);

Clay Shannon  Monday, September 22, 2008 9:03 PM

Hi!

As far as I know, LINQ only works with XMLfiles or SQL Databases or dictionary structures and not with CSV files. Try and transform the CVS into XML and then query it with LINQ.

Anyway, your problem is not that LINQ cannot query CVS, but that you do not corectly open the file. At least in the second example. If it's a CVS file, you should open it as a regular text file, read the text, and then try and split the text you're read.

Regards,

Lucian Baciu  Wednesday, September 24, 2008 10:16 AM
OLE DB cannot read embedded resources. A somewhat workable approach is to save the resource to a file in the AppData directory.

Frankly, using JET to read a .csv formatted text file is somewhat akin to using a cannon to shoot a mosquito. Especially since JET is deprecated and will soon be completely unavailable, it already is on Vista x64. Use the Microsoft.VisualBasic.FileIO.TextFieldParser class. It has a constructor that takes a string argument, just what you need to read the resource without having to save it to a file first.
nobugz  Wednesday, September 24, 2008 7:57 PM

Hi!

As far as I know, LINQ only works with XMLfiles or SQL Databases or dictionary structures and not with CSV files. Try and transform the CVS into XML and then query it with LINQ.

Anyway, your problem is not that LINQ cannot query CVS, but that you do not corectly open the file. At least in the second example. If it's a CVS file, you should open it as a regular text file, read the text, and then try and split the text you're read.

Regards,

Lucian Baciu  Wednesday, September 24, 2008 10:16 AM
OLE DB cannot read embedded resources. A somewhat workable approach is to save the resource to a file in the AppData directory.

Frankly, using JET to read a .csv formatted text file is somewhat akin to using a cannon to shoot a mosquito. Especially since JET is deprecated and will soon be completely unavailable, it already is on Vista x64. Use the Microsoft.VisualBasic.FileIO.TextFieldParser class. It has a constructor that takes a string argument, just what you need to read the resource without having to save it to a file first.
nobugz  Wednesday, September 24, 2008 7:57 PM

You can use google to search for other answers

Custom Search

More Threads

• DirectSound with no DirectX
• Display Timer
• Resources use.
• Visual Studio 2005- help required linking data
• How to print to an ip address?
• Creating Menu from DataSet??
• OnPaint method not firing in my WinForm custom control...
• Merging Menus in .NET
• Browser Control Questions.
• How can I mail merge into a password protected MS Form