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

最新文章

  1. ios结构体语法
  2. Java重点之小白解析--浅谈HashMap与HashTable
  3. 自己yy的fulkson最大流算法
  4. json化 datatable
  5. java开发常用jar包介绍(转载)
  6. 年前辞职-WCF入门学习(5)
  7. 数据结构(左偏树,可并堆):BNUOJ 3943 Safe Travel
  8. C#“简单加密文本器”的实现
  9. python 解析xml
  10. ERP中通过EDI导入资料的时候出现【Microsoft Office Excel不能访问文件‘C:\Windows\TEMP\433....’
  11. Asp.Net Web API(二)
  12. Win64下编译OSG详细过程(Win10+VS2015+OSG3.6.3)
  13. 软件测试第四次作业—— 性能测试(含JMeter实验)
  14. SpringMVC中JSONP的基本使用
  15. Linux定时任务计划
  16. SNK 与PFX
  17. HDU2017新生赛 找方块
  18. cb6xe7代码提示风格变化
  19. Firebird 备份与恢复
  20. python3与mysql交互:pymysql

热门文章

  1. 用 Flask 来写个轻博客
  2. 新生 &amp; 语不惊人死不休 —— 《无限恐怖》读后有感
  3. 孤荷凌寒自学python第五十五天初识MongoDb数据库
  4. Ubuntu16.04安装openCV的问题集合
  5. 条件随机场(Conditional random field)
  6. SQL查询oracle的nclob字段
  7. Oracle 数据库导出时 EXP-00008;ORA-00904
  8. 记一下STL的一个题
  9. ocrosoft Contest1316 - 信奥编程之路~~~~~第三关问题 D: 手机话费
  10. Vue 使用Spread.js没有层级关系(隐藏与显示)