linq for sqlite的使用方法(C#)

1. 添加引用到工程

System.Data.SQLite

System.Data.SQLite.Linq

2. 修改app.config, 如下:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

 

3. 建立与sqlite表对应的实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;

namespace linq_test
{
    [Table(Name = "A")]
    public class A
    {
        [Column(Name = "col_1")]
        public string Col_1 { get; set; }
    }
 
}

 

4. 通过DataContext, 编写查询linq  to sql

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data.Linq;

namespace linq_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var ctx = new DataContext(new SQLiteConnection( "data source=d:\\test.db"));
            Table<A> a = ctx.GetTable<A>();
            var query = from p in a select p;
            foreach (var item in query)
            {
                System.Console.WriteLine("ID:{0}", item.Col_1);
            }

            System.Console.ReadKey();
        }
    }
}

 

 

你可能感兴趣的:(linq for sqlite的使用方法(C#))