1) NuGet 패키지 관리자 -> 패키지 관리자 콘솔 ->
Install-Package System.Data.SQLite (입력)
※ Install-Package System.Data.SQLite.Core (정상 실행이 되지 않으면 Core로 설치)


아래는 예제 코드입니다.
Connection을 유지해도 괜찮지만 가벼운 SQLite 특성상 매번 접근해줘도 성능에 무리는 없다고 하네요.
connectString에서
Data Source=mydb.db
선언을 통해 mydb.db 파일에 접근이 가능해집니다. (파일이 없다면 자동 생성합니다.)
using System.Windows.Forms;
using System.Data.SQLite;
using System.Collections.Generic;
using System;
namespace WindowsFormsApp_SQLite
{
public partial class Form1 : Form
{
class Data
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
private const string connectString = "Data Source=mydb.db;Version=3;";
public Form1()
{
InitializeComponent();
}
private void CreateTable()
{
using (SQLiteConnection connection = new SQLiteConnection(connectString))
{
connection.Open();
string query = @"CREATE TABLE IF NOT EXISTS Users
(Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
Age INTEGER)";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
}
private void Insert(string name, int age)
{
using (SQLiteConnection connection = new SQLiteConnection(connectString))
{
connection.Open();
string query = "INSERT INTO Users (Name, Age) VALUES (@name, @age)";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@age", age);
command.ExecuteNonQuery();
}
}
}
private List<Data> SelectAll()
{
List<Data> dataList = new List<Data>();
using (SQLiteConnection connection = new SQLiteConnection(connectString))
{
connection.Open();
string query = "SELECT * FROM Users";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
dataList.Add(new Data
{
Id = Convert.ToInt32(reader["Id"]),
Name = reader["Name"].ToString(),
Age = Convert.ToInt32(reader["Age"])
});
}
}
}
}
return dataList;
}
private void Update(int id, string name, int age)
{
using (SQLiteConnection connection = new SQLiteConnection(connectString))
{
connection.Open();
string query = "UPDATE Users SET Name=@name, Age=@age WHERE Id=@id";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@id", id);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@age", age);
command.ExecuteNonQuery();
}
}
}
private void Delete(int id)
{
using (SQLiteConnection connection = new SQLiteConnection(connectString))
{
connection.Open();
string query = "DELETE FROM Users WHERE Id=@id";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@id", id);
command.ExecuteNonQuery();
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
CreateTable();
Insert("테스트1", 30);
Insert("테스트2", 20);
textBox1.Text = string.Format("Insert 후 시작{0}", Environment.NewLine);
List<Data> dataList = SelectAll();
PrintData(dataList);
Update(dataList[0].Id, "업데이트1", 70);
Delete(dataList[1].Id);
textBox1.Text += string.Format("업데이트 & 삭제{0}", Environment.NewLine);
dataList = SelectAll();
PrintData(dataList);
//전체 제거
for (int i = 0; i < dataList.Count; ++i)
{
Delete(dataList[i].Id);
}
}
private void PrintData(List<Data> dataList)
{
foreach (Data data in dataList)
{
textBox1.Text += string.Format("Id:{0} Name:{1} Age:{2}{3}", data.Id, data.Name, data.Age, Environment.NewLine);
}
}
}
}'C# > Windows Form' 카테고리의 다른 글
| [C# Windows Form] CustomTreeView 트리뷰 (깜박임 제거, 드래그 앤 드롭, 텍스트 추가 출력 등) (0) | 2026.04.13 |
|---|---|
| [C# Windows Form] 디자이너에서 커스텀 필드 생성하기 (0) | 2026.03.05 |
| [C# Windows Form] PrivateFontCollection 외부 폰트 파일 적용 (1) | 2025.11.28 |
| [C# Windows Form] FormBorderStyle None 에 대한 기본 클래스 (0) | 2025.11.28 |
| [C# Windows Form] Custom Button Class - 이미지 버튼 생성 (hover, press, normal, focus) (0) | 2025.11.27 |











