forked from GavinYellow/SharpSCADA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataHelper.cs
More file actions
240 lines (222 loc) · 8.81 KB
/
Copy pathDataHelper.cs
File metadata and controls
240 lines (222 loc) · 8.81 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
namespace DatabaseLib
{
public static class DataHelper
{
static string m_ConnStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=SharpSCADA;Integrated Security=True";
static string m_Path = @"D:\HDA";
static string m_host = Environment.MachineName;
static string m_type = "MSSQL";
//数据库工厂接口
const string CFGPATH = @"C:\DataConfig\host.cfg";
const string INIPATH = @"C:\DataConfig\host.ini";
const string DATALOGSOURCE = "Data Operations";
const string DATALOGNAME = "Data Log";
const int STRINGMAX = 255;
static EventLog Log;
#region GetInstance
private static IDataFactory _ins;
public static IDataFactory Instance
{
get
{
return _ins;
}
}
public static string HostName
{
get { return m_host; }
}
public static string ConnectString
{
get { return m_ConnStr; }
}
public static string HdaPath
{
get { return m_Path; }
}
#endregion
/// <summary>
/// 数据库工厂构造函数
/// </summary>
/// <param name="dbtype">数据库枚举</param>
static DataHelper()
{
if (!EventLog.SourceExists(DATALOGSOURCE))
EventLog.CreateEventSource(DATALOGSOURCE, DATALOGNAME);
Log = new EventLog(DATALOGNAME);
try
{
if (File.Exists(INIPATH))
{
StringBuilder sb = new StringBuilder(STRINGMAX);
WinAPI.GetPrivateProfileString("HOST", "SERVER", m_host, sb, STRINGMAX, INIPATH);
m_host = sb.ToString();
WinAPI.GetPrivateProfileString("DATABASE", "CONNSTRING", m_ConnStr, sb, STRINGMAX, INIPATH);
m_ConnStr = sb.ToString();
WinAPI.GetPrivateProfileString("DATABASE", "ARCHIVE", m_Path, sb, STRINGMAX, INIPATH);
m_Path = sb.ToString();
WinAPI.GetPrivateProfileString("DATABASE", "TYPE", m_type, sb, STRINGMAX, INIPATH);
m_type = sb.ToString();
}
else if (File.Exists(CFGPATH))
{
using (StreamReader objReader = new StreamReader(CFGPATH))
{
m_host = objReader.ReadLine();
m_ConnStr = objReader.ReadLine();
m_Path = objReader.ReadLine();
}
}
IPAddress addr;
if (string.IsNullOrEmpty(m_host) || !IPAddress.TryParse(m_host, out addr))
{
m_host = Environment.MachineName;
}
switch (m_type.ToUpper())
{
case "MSSQL":
_ins = new MssqlFactory();
break;
case "MYSQL":
_ins = new MysqlFactory();
break;
default:
_ins = new MssqlFactory();
break;
}
}
catch (Exception e)
{
AddErrorLog(e);
}
}
public static DbParameter CreateParam(string paramName, SqlDbType dbType, object objValue, int size = 0, ParameterDirection direction = ParameterDirection.Input)
{
return _ins.CreateParam(paramName, dbType, objValue, size, direction);
}
public static string DataTableToCsv(DataTable table)
{
//以半角逗号(即,)作分隔符,列为空也要表达其存在。
//列内容如存在半角逗号(即,)则用半角引号(即"")将该字段值包含起来。
//列内容如存在半角引号(即")则应替换成半角双引号("")转义,并用半角引号(即"")将该字段值包含起来。
StringBuilder sb = new StringBuilder();
DataColumn colum;
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
colum = table.Columns[i];
if (i != 0) sb.Append(",");
var txt = row[colum] == null ? "" : row[colum].ToString();
if (colum.DataType == typeof(string) && txt.Contains(","))
{
sb.Append("\"" + txt.Replace("\"", "\"\"") + "\"");
}
else sb.Append(txt);
}
sb.AppendLine();
}
return sb.ToString();
}
public static string ReaderToCsv(IDataReader reader)
{
StringBuilder sb = new StringBuilder();
var colcount = reader.FieldCount;
while (reader.Read())
{
for (int i = 0; i < colcount; i++)
{
if (i != 0) sb.Append(",");
var txt = reader[i] == null ? "" : reader[i].ToString();
if (txt.Contains(","))
{
sb.Append("\"" + txt.Replace("\"", "\"\"") + "\"");
}
else sb.Append(txt);
}
sb.AppendLine();
}
return sb.ToString();
}
public static void AddErrorLog(Exception e)
{
string err = "";
Exception exp = e;
while (exp != null)
{
err += string.Format("\n {0}", exp.Message);
exp = exp.InnerException;
}
err += string.Format("\n {0}", e.StackTrace);
Log.Source = DATALOGSOURCE;
Log.WriteEntry(err, EventLogEntryType.Error);
}
public static string GetNullableString(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader != null)
{
var svr = dataReader.GetSqlString(index);
return svr.IsNull ? null : svr.Value;
}
else return reader.GetString(index);
}
public static DateTime? GetNullableTime(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader == null) return reader.GetDateTime(index);
var svr = dataReader.GetSqlDateTime(index);
return svr.IsNull ? default(Nullable<DateTime>) : svr.Value;
}
public static int GetTimeTick(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader != null)
{
return dataReader.GetSqlDateTime(index).TimeTicks;
}
var datetime = reader.GetDateTime(index);
var value = datetime.Subtract(new DateTime(1900, 1, 1));
long num2 = value.Ticks - value.Days * 864000000000;
if (num2 < 0)
num2 += 864000000000;
int num3 = (int)(num2 / 10000.0 * 0.3 + 0.5);
if (num3 > 300 * 60 * 60 * 24 - 1)
num3 = 0;
return num3;
}
public static object GetSqlValue(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader != null)
{
return dataReader.GetSqlValue(index);
}
var mq = reader as MySqlDataReader;
if (mq != null)
{
return mq.GetValue(index);
}
return "";
}
}
public static class WinAPI
{
//参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
//参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
}
}