IT/C#

[C#] SQLite 설치 및 사용하기

Ella.J 2021. 3. 15. 17:26
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 > 0return 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 > 0return 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
반응형