SQLite.cs
6.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
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using TSA_V.Common;
namespace DAL
{
public class SQLite
{
private SQLiteConnection _con;
public SQLite()
{
}
public bool IsCon { private set; get; } = false;
public bool Connect()
{
try
{
_con = new SQLiteConnection("data source='" + Environment.CurrentDirectory + "\\config\\database.db3';Version=3");
_con.Open();
IsCon = true;
LogUtil.info("数据库连接成功");
return true;
}
catch (Exception ex)
{
IsCon = false;
LogUtil.error("数据库连接失败:" + ex.Message);
return false;
}
}
public void Close()
{
if (_con != null)
{
_con.Close();
_con.Dispose();
LogUtil.info("数据库关闭");
}
}
//public bool QueryUserName(string username, string password)
//{
// bool succeed = false;
// string sql = "SELECT * FROM Account WHERE Username='" + username + "' AND Password='" + password + "'";
// bool rtn = Select(sql, out string[][] data);
// if (rtn)
// {
// if (data.Length > 0)
// succeed = true;
// }
// return succeed;
//}
public bool QueryHistory(string barCode, string dateTimeFront, string dateTimeBack, out string[][] data)
{
string sql = "SELECT ID,ProName,ProType,BarCode,BoardWidth,BoardLength,AoiResult,UserName,CreateDate FROM OperateInfo WHERE 1=1";
if (!string.IsNullOrEmpty(barCode))
sql += " AND BarCode LIKE '%" + barCode + "%'";
if (!string.IsNullOrEmpty(TSA_V.DeviceLibrary.DB.userName))
sql += " AND UserName = '" + TSA_V.DeviceLibrary.DB.userName + "'";
if (!string.IsNullOrEmpty(dateTimeFront))
sql += " AND CreateDate >= '" + dateTimeFront + "'";
if (!string.IsNullOrEmpty(dateTimeBack))
sql += " AND CreateDate <= '" + dateTimeBack + "'";
bool rtn = Select(sql, out data);
return rtn;
}
public bool QueryPointInfo(string id, out string[][] data)
{
string sql = "SELECT ID,PartNum,PointName,CreateDate FROM PointInfo WHERE ID=" + id;
bool rtn = Select(sql, out data);
return rtn;
}
public bool AddHistory(TSA_V.DeviceLibrary.OpInfo info, out int id)
{
string sql;
bool rtn;
id = info.ID;
//查询可写入的ID
if (info.ID == 0)
{
sql = "SELECT MAX(ID) FROM PointInfo";
rtn = Select(sql, out string[][] data);
if (!rtn) return false;
int.TryParse(data[0][0], out id);
id++;
}
//添加操作信息记录
string createDate = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
sql = "INSERT INTO OperateInfo(ID,ProName,ProType,BarCode,BoardWidth,BoardLength,AoiResult,UserName,CreateDate) " +
"VALUES(" + id + ",'" + info.ProName + "','" + info.ProType + "','" + info.BarCode + "'," + info.BoardWidth + "," +
info.BoardLength + ",'" + info.AoiResult + "','" + TSA_V.DeviceLibrary.DB.userName + "','" + createDate + "')";
rtn = Execute(sql);
if (!rtn) return false;
//添加操作点信息
if (info.ID == 0)
{
SQLiteTransaction tr = _con.BeginTransaction();
for (int i = 0; i < info.pointList.Count; i++)
{
sql = "INSERT INTO PointInfo(ID,PartNum,PointName,CreateDate) " +
"VALUES(" + id + ",'" + info.pointList[i].PartNum + "','" + info.pointList[i].PointName + "','" + createDate + "')";
rtn = Execute(sql);
if (!rtn) break;
}
LogUtil.info("数据库Commit");
if (rtn)
tr.Commit();
else
tr.Rollback();
LogUtil.info("数据库 完成");
}
return rtn;
}
private bool Select(string sql, out string[][] data)
{
data = null;
if (!IsCon) return false;
List<string[]> content = new List<string[]>();
try
{
SQLiteCommand cmd = new SQLiteCommand(_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();
LogUtil.info("数据库Select,SQL=" + sql);
return true;
}
catch (Exception ex)
{
LogUtil.error("数据库Select:" + ex.Message);
return false;
}
}
private bool Execute(string sql)
{
if (!IsCon) return false;
try
{
SQLiteCommand cmd = new SQLiteCommand(_con) { CommandText = sql };
int n = cmd.ExecuteNonQuery();
cmd.Dispose();
LogUtil.info("数据库Execute,SQL=" + sql + ",修改了" + n + "行");
return true;
}
catch (Exception ex)
{
LogUtil.error("数据库Execute:" + ex.Message);
return false;
}
}
}
}