Http://www.cnblogs.com/xianyin05/archive/2012/12/23/2829905.html
usingModels;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.SQLite;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;namespacedemo2{classProgram {Static voidMain (string[] args) { //Program p = new program ();Sqlitedatacontext db =NewSqlitedatacontext (@"Data Source=mydatabase.sqlite; version=3;");//Create a connection//var str = db.highscores.Where (u=>u.score>3000). ToList ();Db. Log =Console.Out; varTEMP1 = db. Gettable(). ToList (); varTEMP = db. Gettable(); varresult = fromNinchTempSelectN; foreach(varIteminchresult) {Console.WriteLine (item.name); } console.readkey (); } //Database Connectionsqliteconnection m_dbconnection; PublicProgram () {createnewdatabase (); Connecttodatabase (); CreateTable (); Filltable (); Printhighscores (); } //Create an empty database voidcreatenewdatabase () {Sqliteconnection.createfile ("Mydatabase.sqlite"); } //Create a connection to the specified database voidconnecttodatabase () {m_dbconnection=NewSqliteconnection ("Data Source=mydatabase.sqlite; version=3;"); M_dbconnection.open (); } //creates a table in the specified database voidcreatetable () {stringsql ="CREATE TABLE Highscores (name varchar (), score int)"; Sqlitecommand Command=Newsqlitecommand (SQL, m_dbconnection); Command. ExecuteNonQuery (); } //Insert some data voidfilltable () {stringsql ="INSERT into Highscores (name, score) VALUES (' Me ', +)"; Sqlitecommand Command=Newsqlitecommand (SQL, m_dbconnection); Command. ExecuteNonQuery (); SQL="INSERT into Highscores (name, score) VALUES (' myself ', 6000)"; Command=Newsqlitecommand (SQL, m_dbconnection); Command. ExecuteNonQuery (); SQL="INSERT into Highscores (name, score) VALUES (' and I ', 9001)"; Command=Newsqlitecommand (SQL, m_dbconnection); Command. ExecuteNonQuery (); } //Use SQL query statements and display the results voidprinthighscores () {stringsql ="SELECT * from Highscores ORDER BY score Desc"; Sqlitecommand Command=Newsqlitecommand (SQL, m_dbconnection); Sqlitedatareader Reader=command. ExecuteReader (); while(reader. Read ()) {Console.WriteLine ("Name:"+ reader["name"] +"\tscore:"+ reader["score"]); } console.readline (); } }}
usingModels;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.Linq;usingSystem.Data.Linq.Mapping;usingSystem.Data.SQLite;usingSystem.Linq;usingSystem.Text;namespacedemo2{ Public classSqlitedatacontext:datacontext { PublicSqlitedatacontext (stringconnection, Mappingsource Mappingsource):Base(connection, Mappingsource) {} PublicSqlitedatacontext (idbconnection connection, Mappingsource Mappingsource):Base(connection, Mappingsource) {} PublicSqlitedatacontext (stringconnectionString):Base(Newsqliteconnection (connectionString)) { } PublicSqlitedatacontext (idbconnection connection):Base(connection) {}//Public Virtual dbset }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Linq.Mapping;usingSystem.Linq;usingSystem.Text;namespacemodels{[Table (Name="highscores")]//attribute represents a table with the Highscores class and the name Highscores in the database Public classHighscores {//[Column (IsPrimaryKey =true)][Column]//attributes correspond to columns in the database Public stringName {Get;Set; } [Column] Public intScore {Get;Set; } }}
LINQ connects to the SQLite database (LINQ to SQLite). net3.5