如何存取SQLite
如果你還在找SQLite的 ADO.Net driver 的話,別找了。
因為Mono就提供了一個:SQLite at Mono。
不管你是在 Windows 或是在 Linux 上,也不管你是用 Microsoft .Net Framework 或是 Mono,都可以直接拿他的 Mono.Data.SqliteClient.dll 來使用~
使用方法也很簡單:
- 連接字串:"URI=file:/path/to/file,version=3"。URI指定檔案位置,version則是指定SQLite database版本。
- 從使用範例可以看出,跟 .Net framework 提供的 ADO.Net driver 用法並沒有什麼差別(範例摘錄自SQLite at Mono):
using System;
using System.Data;
using Mono.Data.SqliteClient;
public class Test
{
public static void Main(string[] args)
{
string connectionString = "URI=file:SqliteTest.db";
IDbConnection dbcon;
dbcon = (IDbConnection) new SqliteConnection(connectionString);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
// requires a table to be created named employee
// with columns firstname and lastname
// such as,
// CREATE TABLE employee (
// firstname varchar(32),
// lastname varchar(32));
string sql =
"SELECT firstname, lastname " +
"FROM employee";
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
while(reader.Read()) {
string FirstName = reader.GetString (0);
string LastName = reader.GetString (1);
Console.WriteLine("Name: " +
FirstName + " " + LastName);
}
// clean up
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
}
如果你想找一個SQLite管理工具,我個人推薦使用SQLiteSpy,既小又方便而且還免安裝。


0 Responses to "如何存取SQLite"
留下您的意見: