2012年9月17日 星期一

C# 使用 System.Data.SQLite

System.Data.SQLite http://sqlite.phxsoftware.com/ 檔案下載後, 解壓縮, 從中取得 System.Data.SQLite.dll 以下範例來自:http://www.javaeye.com/topic/114055 using using System.Data.SQLite; Connection和Command: private SQLiteConnection conn; private SQLiteCommand cmd; 連接db: conn = new SQLiteConnection("Data Source=c:\\test.db"); conn.Open(); INSERT/UPDATE: cmd = conn.CreateCommand(); cmd.CommandText = "INSERT INTO user(email,name) VALUES ('email','name')"; cmd.ExecuteNonQuery(); cmd.CommandText = "UPDATE userSET name = 'Codelicious' WHERE ID = 1"; cmd.ExecuteNonQuery(); SELECT: cmd.CommandText = "SELECT ID, name FROM user"; SQLiteDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Console.WriteLine("ID: " + reader.GetInt16(0)); Console.WriteLine("name: " + reader.GetString(1)); } }

沒有留言:

張貼留言