linq串連sqlite資料庫(linq to sqlite) .net3.5

來源:互聯網
上載者:User

標籤:查詢   key   sqlite   arch   com   elf   any   value   ted   

http://www.cnblogs.com/xianyin05/archive/2012/12/23/2829905.html

using Models;using System;using System.Collections.Generic;using System.Data.SQLite;using System.Diagnostics;using System.Linq;using System.Text;namespace Demo2{    class Program    {        static void Main(string[] args)        {            //Program p = new Program();            SqliteDataContext db = new SqliteDataContext(@"Data Source=MyDatabase.sqlite;Version=3;");  //建立串連            //var str = db.highscores.Where(u=>u.score>3000).ToList();            db.Log = Console.Out;            var temp1 = db.GetTable<highscores>().ToList();                        var temp = db.GetTable<highscores>();            var result = from n in temp select n;            foreach (var item in result)            {                Console.WriteLine(item.name);            }            Console.ReadKey();        }        //資料庫連接        SQLiteConnection m_dbConnection;        public Program()        {            createNewDatabase();            connectToDatabase();            createTable();            fillTable();            printHighscores();        }        //建立一個空的資料庫        void createNewDatabase()        {            SQLiteConnection.CreateFile("MyDatabase.sqlite");        }        //建立一個串連到指定資料庫        void connectToDatabase()        {            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");            m_dbConnection.Open();        }        //在指定資料庫中建立一個table        void createTable()        {            string sql = "create table highscores (name varchar(20), score int)";            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);            command.ExecuteNonQuery();        }        //插入一些資料        void fillTable()        {            string sql = "insert into highscores (name, score) values (‘Me‘, 3000)";            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);            command.ExecuteNonQuery();            sql = "insert into highscores (name, score) values (‘Myself‘, 6000)";            command = new SQLiteCommand(sql, m_dbConnection);            command.ExecuteNonQuery();            sql = "insert into highscores (name, score) values (‘And I‘, 9001)";            command = new SQLiteCommand(sql, m_dbConnection);            command.ExecuteNonQuery();        }        //使用sql查詢語句,並顯示結果        void printHighscores()        {            string sql = "select * from highscores order by score desc";            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);            SQLiteDataReader reader = command.ExecuteReader();            while (reader.Read())            {                Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);            }            Console.ReadLine();        }    }}
using Models;using System;using System.Collections.Generic;using System.Data;using System.Data.Linq;using System.Data.Linq.Mapping;using System.Data.SQLite;using System.Linq;using System.Text;namespace Demo2{    public class SqliteDataContext : DataContext    {        public SqliteDataContext(string connection, MappingSource mappingSource) :            base(connection, mappingSource)        {        }        public SqliteDataContext(IDbConnection connection, MappingSource mappingSource) :            base(connection, mappingSource)        {        }        public SqliteDataContext(string connectionString) :            base(new SQLiteConnection(connectionString))        {        }        public SqliteDataContext(IDbConnection connection) :            base(connection)        {        }        //public virtual DbSet<highscores> highscores { get; set; }    }}
using System;using System.Collections.Generic;using System.Data.Linq.Mapping;using System.Linq;using System.Text;namespace Models{    [Table(Name = "highscores")]  //特性表示將highscores類與資料庫中名稱為highscores的表    public class highscores    {        //[Column(IsPrimaryKey =true)]        [Column]  //特性則對應資料庫中的列        public string name { get; set; }        [Column]        public int score { get; set; }    }}

 

linq串連sqlite資料庫(linq to sqlite) .net3.5

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.