-
Notifications
You must be signed in to change notification settings - Fork 0
/
O_MSSQL.cs
127 lines (114 loc) · 3.86 KB
/
O_MSSQL.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SISCell
{
class O_MSSQL : oProtocol
{
private INI cfg;
private LOG err;
private string _server, _dataBase, _user, _password, _concmd;
private SqlConnection _con;
private string _tblName, _tagField, _valField, _dtmField;
private bool _connected = false;
public O_MSSQL()
{
cfg = new INI(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "config\\sql.ini");
err = new LOG();
_server = cfg.GetVal("Connect", "Server");
_dataBase = cfg.GetVal("Connect", "Database");
_user = cfg.GetVal("Connect", "User");
_password = cfg.GetVal("Connect", "Password");
StringBuilder cmd = new StringBuilder(string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", _server, _dataBase, _user, _password));
_concmd = cmd.ToString();
_tblName = cfg.GetVal("Output", "Table");
_tagField = cfg.GetVal("Output", "tag");
_valField = cfg.GetVal("Output", "val");
_dtmField = cfg.GetVal("Output", "dtm");
}
#region oProtocol 成员
public bool Connected
{
get { return _connected; }
}
public bool Connect()
{
try
{
_con = new SqlConnection(_concmd);
_con.Open();
_connected = true;
return true;
}
catch (Exception ex)
{
err.WrtMsg(ex.Message);
return false;
}
}
public void DisConnect()
{
_con.Close();
_connected = false;
}
public void InitPt(int nNum, numInf[] nrst, int sNum, strInf[] srst)
{
}
public void SetRtValue(int nNum, numInf[] nrst, int sNum, strInf[] srst)
{
SqlCommand cmd = new SqlCommand(string.Format("TRUNCATE TABLE {0}", _tblName), _con);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
err.WrtMsg(ex.Message);
_connected = false;
}
using (DataTable dtbl = new DataTable())
{
dtbl.Columns.Add("tag", typeof(string));
dtbl.Columns.Add("val", typeof(string));
dtbl.Columns.Add("dtm", typeof(DateTime));
DataRow drow;
foreach (numInf nr in nrst)
{
drow = dtbl.NewRow();
drow["tag"] = nr.dstId;
drow["val"] = nr.val.ToString();
drow["dtm"] = nr.dtm;
dtbl.Rows.Add(drow);
}
foreach (strInf sr in srst)
{
drow = dtbl.NewRow();
drow["tag"] = sr.dstId;
drow["val"] = sr.val;
drow["dtm"] = sr.dtm;
dtbl.Rows.Add(drow);
}
using (SqlBulkCopy bcp = new SqlBulkCopy(_con))
{
bcp.DestinationTableName = _tblName;
bcp.ColumnMappings.Add("tag", _tagField);
bcp.ColumnMappings.Add("val", _valField);
bcp.ColumnMappings.Add("dtm", _dtmField);
try
{
bcp.WriteToServer(dtbl);
}
catch (Exception ex)
{
err.WrtMsg(ex.Message);
_connected = false;
}
}
}
}
#endregion
}
}