Skip to content

Instantly share code, notes, and snippets.

@ruyut
Last active December 27, 2021 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruyut/3ad0985167006a4756be43405154f834 to your computer and use it in GitHub Desktop.
Save ruyut/3ad0985167006a4756be43405154f834 to your computer and use it in GitHub Desktop.
C# SQLite CRUD example
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Microsoft.Data.Sqlite;
namespace RuyutWinFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateDatabaseFile();
Insert("Ruyut");
Read();
Update(1, "Ruyut_1234");
Read();
Delete(1);
Read();
}
/// <summary>
/// 資料庫連接字串
/// </summary>
private string _connectionString = "Data Source=db.db;";
/// <summary>
/// 建立資料庫檔案
/// </summary>
private void CreateDatabaseFile()
{
Debug.WriteLine("CreateDatabaseFile");
if (File.Exists("db.db")) return;
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"CREATE TABLE users (
id INTEGER,
user_name TEXT NOT NULL UNIQUE,
PRIMARY KEY(id AUTOINCREMENT)
);";
command.ExecuteNonQuery();
}
}
/// <summary>
/// 新增
/// </summary>
/// <param name="userName"></param>
private void Insert(string userName)
{
Debug.WriteLine("Insert");
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@" INSERT INTO users (user_name)
values ($userName);
select last_insert_rowid();";
command.Parameters.AddWithValue("$userName", userName);
int id = Convert.ToInt32((object)command.ExecuteScalar());
Debug.WriteLine($"\tid = {id}, userName = {userName}");
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="id"></param>
/// <param name="userName"></param>
private void Update(int id, string userName)
{
Debug.WriteLine("Update");
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@" UPDATE users
SET user_name= $userName
WHERE id = $id;";
command.Parameters.AddWithValue("$userName", userName);
command.Parameters.AddWithValue("id", id);
command.ExecuteNonQuery();
}
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="id"></param>
private void Delete(int id)
{
Debug.WriteLine("Delete");
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@" delete from users where id = $id;";
command.Parameters.AddWithValue("id", id);
command.ExecuteNonQuery();
}
}
/// <summary>
/// 查詢、讀取
/// </summary>
private void Read()
{
Debug.WriteLine("Read");
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = @" select * from users";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var id = reader.GetString(0);
var name = reader.GetString(1);
Debug.WriteLine($"\tid = {id}, name = {name}");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment