hytos / DTI_PID / BaseModel / Project_DB.cs @ 0d8062b2
이력 | 보기 | 이력해설 | 다운로드 (4.41 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using System.Globalization; |
7 |
using System.Data.SQLite; |
8 |
using System.Data; |
9 |
|
10 |
namespace Converter.BaseModel |
11 |
{ |
12 |
public class Project_DB |
13 |
{ |
14 |
const string SPPID_DB_INFO_TABLE = "SPPID_CONNECTION_INFO"; |
15 |
|
16 |
public static void Conn() |
17 |
{ |
18 |
Project_Info projectInfo = Project_Info.GetInstance(); |
19 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
20 |
{ |
21 |
connection.Open(); |
22 |
} |
23 |
} |
24 |
|
25 |
public static bool ConnTestAndCreateTable() |
26 |
{ |
27 |
bool result = false; |
28 |
Project_Info projectInfo = Project_Info.GetInstance(); |
29 |
SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)); |
30 |
try |
31 |
{ |
32 |
connection.Open(); |
33 |
if (connection.State == ConnectionState.Open) |
34 |
{ |
35 |
CreateTable(connection); |
36 |
result = true; |
37 |
} |
38 |
connection.Close(); |
39 |
} |
40 |
catch (Exception ex) |
41 |
{ |
42 |
|
43 |
} |
44 |
finally |
45 |
{ |
46 |
connection.Dispose(); |
47 |
} |
48 |
|
49 |
return result; |
50 |
} |
51 |
|
52 |
public static bool SaveSPPID_DB_INFO(string jsonString) |
53 |
{ |
54 |
Project_Info projectInfo = Project_Info.GetInstance(); |
55 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
56 |
{ |
57 |
|
58 |
try |
59 |
{ |
60 |
connection.Open(); |
61 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
62 |
{ |
63 |
cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE); |
64 |
cmd.ExecuteNonQuery(); |
65 |
|
66 |
cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE); |
67 |
cmd.Parameters.AddWithValue("@jsonString", jsonString); |
68 |
cmd.ExecuteNonQuery(); |
69 |
} |
70 |
connection.Close(); |
71 |
} |
72 |
catch (Exception ex) |
73 |
{ |
74 |
return false; |
75 |
} |
76 |
finally |
77 |
{ |
78 |
connection.Dispose(); |
79 |
} |
80 |
} |
81 |
|
82 |
return true; |
83 |
} |
84 |
|
85 |
public static DataTable SelectSPPID_DB_INFO() |
86 |
{ |
87 |
DataTable dt = new DataTable(); |
88 |
Project_Info projectInfo = Project_Info.GetInstance(); |
89 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
90 |
{ |
91 |
|
92 |
try |
93 |
{ |
94 |
connection.Open(); |
95 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
96 |
{ |
97 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE); |
98 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
99 |
dt.Load(dr); |
100 |
} |
101 |
connection.Close(); |
102 |
} |
103 |
catch (Exception ex) |
104 |
{ |
105 |
|
106 |
} |
107 |
finally |
108 |
{ |
109 |
connection.Dispose(); |
110 |
} |
111 |
} |
112 |
|
113 |
return dt; |
114 |
} |
115 |
|
116 |
private static void CreateTable(SQLiteConnection connection) |
117 |
{ |
118 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
119 |
{ |
120 |
cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'"; |
121 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
122 |
using (DataTable dt = new DataTable()) |
123 |
{ |
124 |
dt.Load(dr); |
125 |
|
126 |
if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0) |
127 |
{ |
128 |
cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE); |
129 |
cmd.ExecuteNonQuery(); |
130 |
} |
131 |
} |
132 |
} |
133 |
} |
134 |
} |
135 |
} |