728x90
반응형
[NuGet Package 추가하기]
도구 -> NuGet 패키지 관리자 -> 패키지 관리자 콘솔
Install-Package System.Data.SQLite 입력하여 설치
[SQLite.cs]
Select, Insert, Update, Delete 함수 포함 클래스
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
using System;
using System.Data.SQLite;
using System.Data;
using System.Windows.Forms;
namespace AccountBook
{
class SQLite
{
string DBpath = Application.StartupPath + @"\database.db";
string dataSource = $@"Data Source={DBpath}";
SQLiteDataAdapter adpt;
public DataSet SelectAll(string table)
{
try
{
DataSet ds = new DataSet();
string sql = $"SELECT * FROM {table}";
adpt = new SQLiteDataAdapter(sql, dataSource);
adpt.Fill(ds, table);
if (ds.Tables.Count > 0) return ds;
else return null;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
public DataSet SelectDetail(string table, string condition, string where = "")
{
try
{
DataSet ds = new DataSet();
string sql = $"SELECT {condition} FROM {table} {where}";
adpt = new SQLiteDataAdapter(sql, dataSource);
adpt.Fill(ds, table);
if (ds.Tables.Count > 0) return ds;
else return null;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
public void Insert(string table, string value)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection(dataSource))
{
conn.Open();
string sql = $"INSERT INTO {table} VALUES ({value})";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
public void Update(string table, string setvalue, string wherevalue = "")
{
try
{
using (SQLiteConnection conn = new SQLiteConnection(dataSource))
{
conn.Open();
string sql = $"UPDATE {table} SET {setvalue} WHERE {wherevalue}";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
public void DeleteAll(string table)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection(dataSource))
{
conn.Open();
string sql = $"DELETE FROM {table}";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
public void DeleteDetail(string table, string wherecol, string wherevalue)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection(dataSource))
{
conn.Open();
string sql = $"DELETE FROM {table} WHERE {wherecol}='{wherevalue}'";
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
}
}
|
cs |
728x90
반응형
'IT > C#' 카테고리의 다른 글
[C#] 윈도우 그림판 기능 추가해보기 (PPT처럼 도형과 도형을 직선으로 잇기) (0) | 2021.03.31 |
---|---|
[C#] MySQL to SQLite Converter (0) | 2021.03.25 |
[C#] MySql DB 연동하여 로그인 구현하기 (feat. 카카오톡 PC 버전 UI) (39) | 2020.12.04 |
[C#] WinForm TextBox Placeholder 적용하기 (+ textBox 비밀번호 대체문자 적용하기) (0) | 2020.11.26 |
[C#, MySql] 가계부 프로그램 다운로드._.프로그램 소스파일 공유 (0) | 2020.10.21 |