C#操作sqlite Database
C#下sqlite操作驱动dll下载:System.Data.SQLite
C#使用sqlite步骤:
(1)新建一个project
(3)使用API操作sqlite DataBase
using System;
using System.Data.sqlite;
namespace sqliteSamples
{
class Program
{
//数据库连接
sqliteConnection m_dbConnection;
static void Main(string[] args)
{
Program p = new Program();
}
public Program()
{
createNewDatabase();
connectToDatabase();
createTable();
fillTable();
printHighscores();
}
//创建一个空的数据库
void createNewDatabase()
{
sqliteConnection.CreateFile("MyDatabase");
//默认生成的数据库文件和System.Data.sqlite.dll在同一路径下
}
//创建一个连接到指定数据库
void connectToDatabase()
{
m_dbConnection = new sqliteConnection("Data Source=MyDatabase;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,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();
}
}
}
关于sqlite的connection string说明:http://www.connectionstrings.com/sqlite/
sqlite GUI客户端列表:http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
sqlite Administrator下载地址:http://download.orbmu2k.de/files/sqliteadmin.zip