SQLite.cs
3.4 KB
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
using System;
using System.Collections.Generic;
using System.Data.SQLite;
namespace DAL
{
public class SQLite
{
private SQLiteConnection con;
public SQLite()
{
}
public bool IsCon { private set; get; } = false;
//public bool Create()
//{
// try
// {
// con = new SQLiteConnection("data source=" + Model.FilePath.CONFIG_DATABASE);
// SQLiteCommand cmd = new(con);
// con.Open();
// cmd.CommandText = "CREATE TABLE SN ([Text] STRING NOT NULL UNIQUE, [Count] INT NOT NULL)";
// cmd.ExecuteNonQuery();
// cmd.CommandText = "CREATE TABLE History ([QRCode] STRING, [LabelName] STRING, [BarCode] STRING, [PrintKey] STRING, [CreateTime] STRING)";
// cmd.ExecuteNonQuery();
// cmd.Dispose();
// con.Close();
// con.Dispose();
// return true;
// }
// catch (Exception ex)
// {
// return false;
// }
//}
public bool Connect()
{
try
{
con = new SQLiteConnection("data source='" + Model.FilePath.CONFIG_DATABASE + "';Version=3");
Model.LogNet.log.Debug($"SQLite ConnectionString = {con.ConnectionString}");
con.Open();
IsCon = true;
Model.LogNet.log.Debug("SQLite数据库连接成功");
}
catch (Exception ex)
{
IsCon = false;
Model.LogNet.log.Error("数据库连接失败", ex);
}
return IsCon;
}
public void Close()
{
if (con != null)
{
con.Close();
con.Dispose();
}
Model.LogNet.log.Debug("SQLite数据库关闭");
}
public bool Select(string sql, out string[][] data)
{
data = null;
if (!IsCon) return false;
List<string[]> content = new();
try
{
SQLiteCommand cmd = new(con) { CommandText = sql };
SQLiteDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string[] s = new string[dr.FieldCount];
for (int i = 0; i < s.Length; i++)
s[i] = dr[i].ToString();
content.Add(s);
}
dr.Close();
cmd.Dispose();
data = content.ToArray();
Model.LogNet.log.Debug("Select,SQL = " + sql);
return true;
}
catch (Exception ex)
{
Model.LogNet.log.Error("Select", ex);
return false;
}
}
public bool Execute(string sql)
{
if (!IsCon) return false;
try
{
SQLiteCommand cmd = new(con) { CommandText = sql };
int n = cmd.ExecuteNonQuery();
cmd.Dispose();
Model.LogNet.log.Info("Execute,SQL = " + sql);
return true;
}
catch (Exception ex)
{
Model.LogNet.log.Error("Execute", ex);
return false;
}
}
}
}