在C#中操作SQLite數據庫需要引用System.Data.SQLite
命名空間,然后使用SQLiteConnection和其他相關類來連接、查詢和操作數據庫。以下是一個簡單的示例:
using System.Data.SQLite;
string connectionString = "Data Source=mydatabase.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(connectionString);
connection.Open();
SQLiteCommand command = new SQLiteCommand("SELECT * FROM mytable", connection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["column1"].ToString());
}
SQLiteCommand insertCommand = new SQLiteCommand("INSERT INTO mytable (column1, column2) VALUES ('value1', 'value2')", connection);
insertCommand.ExecuteNonQuery();
SQLiteCommand updateCommand = new SQLiteCommand("UPDATE mytable SET column1='newvalue' WHERE id=1", connection);
updateCommand.ExecuteNonQuery();
SQLiteCommand deleteCommand = new SQLiteCommand("DELETE FROM mytable WHERE id=1", connection);
deleteCommand.ExecuteNonQuery();
connection.Close();
以上是一個簡單的示例,根據實際需求可能需要更復雜的操作,但基本操作原理都是類似的。希望對你有幫助。