tags:

views:

491

answers:

8

I would like to store values in a text file as comma or tab seperated values (it doesn't matter).

I am looking for a reusable library that can manipulate this data in this text file, as if it were a sql table.

I need select * from... and delete from where ID = ...... (ID will be the first column in the text file).

Is there some code plex project that does this kind of thing?

I do not need complex functionality like joining or relationships. I will just have 1 text file, which will become 1 database table.

+2  A: 

Use ODBC. There is a Microsoft Text Driver for csv-Files. I think this would be possible. I don't have tested if you can manipulate via ODBC, but you can test it easily. For querying you can also use linq.

martin
Oops I should have said in the question, I don't want to use odbc, because that means extra management and deployment hassles. But thank you for this suggestion.
JL
@JL: What would you need to deploy if use ODBC? Do you deploy anything when you use Access database files using ODBC?
Hemant
@Hermant - this requirement is an after thought, and is used for storing internal stuff to the app, because of this, I don't want to create yet another deployment step. I was thinking of using a dataset and just serializing this to a text file.
JL
+3  A: 

Use LINQ to CSV.

http://www.codeproject.com/KB/linq/LINQtoCSV.aspx

http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-CSV-using-DynamicObject.aspx

If its not CSV in that case

Let your file hold one record per line. Each record at runtime should be read into a Collection of type Record [assuming Record is custom class representing individual record]. You can do LINQ operations on the collection and write back the collection into file.

this. __curious_geek
sounds interesting, any code samples welcome...
JL
Available on the Links.
this. __curious_geek
+1  A: 

Have you looked at the FileHelpers library? It has the capability of reading and parsing a text file into CLR objects. Combining that with the power of something like LINQ to Objects, you have the functionality you need.

Agent_9191
roflol - dude is that link correct? Winter Jersey sale?
JL
Whoops. The problem of answering questions when you're partly awake and fingers aren't helping.
Agent_9191
+1  A: 
public class Item
{
    public int ID { get; set; }
    public string Type { get; set; }
    public string Instance { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string[] lines = File.ReadAllLines("database.txt");

        var list = lines
                .Select(l =>
                    {
                        var split = l.Split(',');
                        return new Item
                        {
                            ID = int.Parse(split[0]),
                            Type = split[1],
                            Instance = split[2]
                        };
                    });

        Item secondItem = list.Where(item => item.ID == 2).Single();

        List<Item> newList = list.ToList<Item>();
        newList.RemoveAll(item => item.ID == 2);

        //override database.txt with new data from "newList"
    }
}

What about data delete. LINQ for query, not for manipulation. However, List provides a predicate-based RemoveAll that does what you want:

newList.RemoveAll(item => item.ID == 2);

Also you can overview more advanced solution "LINQ to Text or CSV Files": http://www.codeproject.com/KB/linq/Linq2CSV.aspx

StreamT
A: 

I would also suggest to use ODBC. This should not complicate the deployment, the whole configuration can be set in the connection-string so you do not need a DSN.

Together with a schema.ini file you can even set column names and data-types, check this KB article from MS.

Vinz
+1  A: 

SQLite

:)

Drevak
A: 

sqllite or linq to text and csv :)

Aneef
A: 

I guess you can use Array List as a structure and save it in your file like programming in C language. What do you think?

Ali Abbaspour