English 中文(简体)
C# resuable library to treat a text file like a database table?
原标题:

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.

最佳回答

SQLite

:)

问题回答

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.

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.

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.

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"

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签