LINQ connects to the SQLite database (LINQ to SQLite). net3.5

Source: Internet
Author: User
Tags sqlite database

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.