sqlitedblinq

Code sample using DBLinq, SQLite


Would someone be able to post a small code sample that has a working connection using DBLinq, SQLite? I have been struggling to get this up and running in a VS 2010 WPF environment for 2 days now. I think I have the connection string worked out, but would love to see a sample up and running.

var con = new SQLiteConnection("DbLinqProvider=Sqlite;Version=3;Data Source=c:\\temp\\testdb.db3;");
DataSource db = new DataSource(con);

var q = from c in db.Person 
        select c;

foreach (Person tempPerson1 in q)
    MessageBox.Show(tempPerson1.Name);

My DBML file(relevant code) - I changed the "Main" to "DataSource", and the SQLite to System.Data.SQLite.SQLiteConnection to have it compile.

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="DataSource")]
[global::System.Data.Linq.Mapping.ProviderAttribute(typeof(System.Data.SQLite.SQLiteConnection))]
public DbLinq.Data.Linq.Table<Person> Person {
    get {
        return this.GetTable<Person>();
    }
}

[global::System.Data.Linq.Mapping.TableAttribute(Name="Datasource.Person")]
public partial class Person {
    private string _id;
    private string _name;

    public Person() { }

    [global::System.Data.Linq.Mapping.ColumnAttribute(
            Name="id", Storage="_id", DbType="VARCHAR(10)")]
    public string ID {
        get {
            return this._id;
        }
        set {
            if ((this._id != value)) {
                this._id = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(
            Name="name", Storage="_name", DbType="VARCHAR(25)")]
    public string Name {
        get {
            return this._name;
        }
        set {
            if ((this._name != value)) {
                this._name = value;
            }
        }
    }
}

I am currently getting a SQLite error that there is no such table: Datasource.Person and I beleive I have the path and connect string correct. Should I have produced both a DBML file and a CS file from the DBMetal?


Solution

  • Solution: I regenerated the DBML file, didn't change the "main" to a different name, included a reference to "Using System.Data.SQLite" and changed

    [global::System.Data.Linq.Mapping.ProviderAttribute(typeof(Sqlite))]
    

    to

    [global::System.Data.Linq.Mapping.ProviderAttribute(typeof(SQLiteConnection))]
    

    Seems to be working now, I am finally getting results from my DB.