SQLite.cs
5.0 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Data.SQLite;
namespace BLL
{
/// <summary>
/// SQLite数据库
/// </summary>
public class SQLite
{
private string _path;
private SQLiteConnection _con;
/// <summary>
/// SQLite数据库
/// </summary>
/// <param name="path"></param>
public SQLite(string path)
{
_path = path;
if (!System.IO.File.Exists(path))
Create();
}
/// <summary>
/// 是否连接
/// </summary>
public bool IsCon { private set; get; } = false;
/// <summary>
/// 错误信息
/// </summary>
public string ErrInfo { private set; get; }
/// <summary>
/// 创建数据库
/// </summary>
/// <returns></returns>
public bool Create()
{
try
{
_con = new SQLiteConnection("data source=" + _path);
SQLiteCommand cmd = new SQLiteCommand(_con);
_con.Open();
cmd.CommandText = "CREATE TABLE Retrospect (ID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,QRCode STRING,Height INT,Width INT,Count INT,ImagePath STRING,CreateDate DATETIME)";
cmd.ExecuteNonQuery();
cmd.Dispose();
_con.Close();
_con.Dispose();
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 连接到数据库
/// </summary>
/// <returns></returns>
public bool Connect()
{
try
{
_con = new SQLiteConnection("data source=" + _path);
_con.Open();
ErrInfo = "OK";
IsCon = true;
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
IsCon = false;
return false;
}
}
/// <summary>
/// 关闭连接数据库
/// </summary>
public void Close()
{
if (_con != null)
{
_con.Close();
_con.Dispose();
}
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="qrCode"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="count"></param>
/// <param name="imagePath"></param>
/// <returns></returns>
public int Insert(string qrCode, int width, int height, int count, string imagePath)
{
if (!IsCon) return -1;
string sql = "INSERT INTO Retrospect([QRCode],[Height],[Width],[Count],[ImagePath],[CreateDate]) VALUES('" + qrCode + "'," + height + "," + width + "," + count + ",'" + imagePath + "','" + string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + "')";
try
{
SQLiteCommand cmd = new SQLiteCommand(_con) { CommandText = sql };
int n = cmd.ExecuteNonQuery();
cmd.Dispose();
ErrInfo = "OK";
return n;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return -1;
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="qrcode"></param>
/// <param name="dataLower"></param>
/// <param name="dataUpper"></param>
/// <param name="data"></param>
/// <returns></returns>
public bool Select(string qrcode, string dataLower, string dataUpper, out string[][] data)
{
data = null;
if (!IsCon) return false;
string sql = "SELECT [QRCode],[Height],[Width],[Count],[ImagePath],[CreateDate] FROM Retrospect WHERE";
sql += " [QRCode] LIKE '%" + qrcode + "%'";
sql += " AND [CreateDate]>='" + dataLower + "'";
sql += " AND [CreateDate]<='" + dataUpper + "'";
try
{
SQLiteCommand cmd = new SQLiteCommand(_con) { CommandText = sql };
SQLiteDataReader dr = cmd.ExecuteReader();
List<string[]> content = new List<string[]>();
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();
ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
}
}