linq连接sqlite数据库(linq to sqlite) .net3.5

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; }
    }
}

 

转载于:https://www.cnblogs.com/genesis/p/7348479.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值