代码片段(1)[全屏查看所有代码]
1. [文件] DatabaseChange.cs ~ 5KB 下载(0)
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
185
186
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Timers;
using
System.Data;
using
System.Data.SqlClient;
using
jd.code.DAO;
namespace
jd.Class
{
/// <summary>
/// 检测数据库是否有更新
/// </summary>
public
class
DatabaseChange
{
#region 0---公共变量
private
System.Timers.Timer timer =
new
System.Timers.Timer(500);
private
SqlConnection sqlConnection;
#endregion
#region 1---构造函数
public
DatabaseChange(SqlConnection sqlConnection)
{
this
.State =
false
;
this
.sqlConnection = sqlConnection;
timer.AutoReset =
true
;
timer.Elapsed +=
new
System.Timers.ElapsedEventHandler(timer_Elapsed);
}
#endregion
#region 2---数据读写器与变量
//=================================================================
/// <summary>
/// 程序是否在运行
/// </summary>
public
bool
State
{
get
{
return
_State; }
set
{ _State = value; }
}
private
bool
_State;
//=================================================================
/// <summary>
/// 返回的数据[0]
/// </summary>
public
bool
DBValue
{
get
{
return
_DBValue; }
set
{ _DBValue = value; }
}
private
bool
_DBValue;
//=================================================================
/// <summary>
/// 返回的数据[1]
/// </summary>
public
string
DBTableName
{
get
{
return
_DBTableName; }
set
{ _DBTableName = value; }
}
private
string
_DBTableName;
#endregion
#region 3---委托与事件
public
delegate
void
SetValues(
bool
value,
string
tableName);
//设置当前数据的委托
/// <summary>
/// 数据更改处理委托
/// </summary>
public
delegate
void
dbChangeEventHandler(
object
sender, dbChangeArgs e);
/// <summary>
/// 数据更改时处理
/// </summary>
public
event
dbChangeEventHandler dbChange;
#endregion
#region 4---方法
//=================================================================
/// <summary>
/// 时钟间隔引发的事件进行处理
/// </summary>
private
void
timer_Elapsed(
object
sender, ElapsedEventArgs e)
{
try
{
OpenConnection();
//打开数据库连接
string
sql =
@"select top 1 * from DBC"
;
SqlDataReader tmpSDR = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql);
if
(tmpSDR.Read())
{
SetThisValues((
bool
)tmpSDR[0], tmpSDR[1].ToString());
}
tmpSDR.Close();
}
catch
(Exception)
{
throw
;
}
}
//=================================================================
/// <summary>
/// 打开数据库连接
/// </summary>
private
void
OpenConnection()
{
try
{
if
(sqlConnection !=
null
)
{
if
(sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
else
if
(sqlConnection.State == ConnectionState.Broken)
{
sqlConnection.Close();
sqlConnection.Open();
}
}
}
catch
(Exception)
{
throw
;
}
}
//=================================================================
private
void
SetThisValues(
bool
value,
string
tableName)
{
if
(
this
.DBValue == value &&
this
.DBTableName == tableName)
{
}
else
{
this
.DBValue = value;
this
.DBTableName = tableName;
dbChange(
null
,
new
dbChangeArgs(value, tableName));
}
}
#endregion
#region 5------主方法------
/// <summary>
/// 启动或暂停
/// </summary>
public
void
Run()
{
if
(timer.Enabled)
{
timer.Stop();
this
.State =
false
;
}
else
{
timer.Start();
this
.State =
true
;
}
}
#endregion
}
/// <summary>
/// 定义事件数据
/// </summary>
public
class
dbChangeArgs : EventArgs
{
public
dbChangeArgs(
bool
value,
string
tableName)
{
this
.Value = value;
this
.TableName = tableName;
}
public
bool
Value {
get
;
private
set
; }
public
string
TableName {
get
;
set
; }
}
}
|