DbLinq - The easy way

2 replies [Last post]
Posts: 2

I wanted to learn to program a MySql databases in case i needed to in the future, but each time I tried to learn i gave up. I hated the fact I hate to "query" things to pull or write information from databases. I wanted to access a database like I access classes. Then I found out about this library called DbLing. As their web page state "DbLinq is THE LINQ provider that allows to use common databases with an API close to Linq to SQL." DbLinq has a tool, called DbMetal, which can automatically convert a MySql (or Oracle, PostgreSQL, SQLite, Ingres, Firebird) database into a c# class which can easily to read and write to database tables.

I read from tables like so:

    class DbTest
    {
        void ReadTest()
        {
            //MyDataContex is a class  auto-generated from the DbLinq tool, DbMetal.
            MyDataContex db = new MyDataContex();
 
            //Accounts is a auto-generated class that represents a database table called "Accounts."
            var ac = from a in db.Accounts
                     select a;
 
            List<string> st = new List<string>();
            foreach (var a in ac)
            {
                //AccountID, UserID, and Password are fields in the table Accounts.
                st.Add(a.AccountID + ", " + a.UserID + ", " + a.Password);
            }
 
            System.IO.File.WriteAllLines(@"C:\dev\db-test.txt", st.ToArray());
        }
    }

As for writing to a table:

 class DbTest
    {
        void WriteTest()
        {
            MyDataContex db = new MyDataContex();
            db.Accounts.InsertOnSubmit(new Accounts() { AccountID = 0, Password = "test", UserID = 0 });
            db.SubmitChanges();
        }
    }

What do you guys think? I want to also add that DbLinq is compatible with Mono which means it is cross-platform. Also, why doesn't NetGore use it, or something similar.

Site: http://code.google.com/p/dblinq2007/

Posts: 164

There are a lot of similar things. (FluentNHibernate for example)

I think NetGore does some kind of code generation. Not sure what it encapsulates and how much is actually done.

Posts: 1691

NetGore doesn't use LINQ to SQL just since I didn't like how it handled relations at all. It didn't really give you much flexibility and forced you to do things a pretty specific way, which sucked. So I wrote my own, which basically just add strong typing to databases, which was pretty much all I wanted. Though it also allows you to treat multiple columns as a collection, as you can see with the stat columns.