-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.cs
283 lines (258 loc) · 9.13 KB
/
db.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dblib
{
//classes DBConnection, DBManager
public class DBConnection
{
public String conntype;
public bool eof = false;
public bool inUse = false;
public int fieldCount = 0;
private object conn;
private object cmd;
private object rdr;
private String UserID = "";
private String Password = "";
private String as400 = "10.0.2.1";
private String tmt = "10.0.2.20";
private String tmtUser = "sa";
private String tmtPass = "sapassword";
public DBConnection(String dbtype = "400")
{
conntype = dbtype.ToUpper();
switch (dbtype)
{
case "400":
conn = new ADODB.Connection();
(conn as ADODB.Connection).Open($"Driver=IBM i Access ODBC Driver;QueryTimeOut=0;System={as400};Uid={UserID};Pwd={Password};Translate Binary=true;",
UserID, Password, 0);
rdr = new ADODB.Recordset();
(rdr as ADODB.Recordset).CursorLocation = ADODB.CursorLocationEnum.adUseClient;
break;
case "TMT":
conn = new SqlConnection($"server={tmt};user id={tmtUser};password={tmtPass};initial catalog=TMWAMS");
break;
}
inUse = true;
}
public bool RunSql(String sql)
{
bool result = true;
eof = false;
sql = sql.Replace("\n", " ").Replace("\r", "").Replace("\t", "").Trim();
switch (conntype)
{
case "400":
if ((conn as ADODB.Connection).State == 1)
(conn as ADODB.Connection).Close();
if ((conn as ADODB.Connection).State == 0)
(conn as ADODB.Connection).Open();
(rdr as ADODB.Recordset).Open(sql, (conn as ADODB.Connection),
ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, -1);
fieldCount = (rdr as ADODB.Recordset).Fields.Count;
eof = (rdr as ADODB.Recordset).EOF;
break;
default:
if ((conn as SqlConnection).State == ConnectionState.Open)
(conn as SqlConnection).Close();
if ((conn as SqlConnection).State == ConnectionState.Closed)
(conn as SqlConnection).Open();
cmd = new SqlCommand(sql, (conn as SqlConnection));
rdr = (cmd as SqlCommand).ExecuteReader();
fieldCount = (rdr as SqlDataReader).FieldCount;
if (!(rdr as SqlDataReader).Read())
eof = true;
break;
}
inUse = true;
return result;
}
public bool RunSql(String fmt, params Object[] objs)
{
return RunSql(String.Format(fmt, objs));
}
public bool ExecSql(String sql)
{
bool result = true;
eof = true;
fieldCount = 0;
sql = sql.Replace("\n", " ").Replace("\r", "").Replace("\t", "").Trim();
switch (conntype)
{
case "400":
if ((conn as ADODB.Connection).State == 1)
(conn as ADODB.Connection).Close();
if ((conn as ADODB.Connection).State == 0)
(conn as ADODB.Connection).Open();
(rdr as ADODB.Recordset).Open(sql, (conn as ADODB.Connection),
ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, -1);
break;
default:
if ((conn as SqlConnection).State == ConnectionState.Open)
(conn as SqlConnection).Close();
if ((conn as SqlConnection).State == ConnectionState.Closed)
(conn as SqlConnection).Open();
cmd = new SqlCommand(sql, (conn as SqlConnection));
rdr = (cmd as SqlCommand).ExecuteReader();
break;
}
inUse = true;
return result;
}
public bool ExecSql(String fmt, params Object[] objs)
{
return ExecSql(String.Format(fmt, objs));
}
public void MoveNext()
{
switch (conntype)
{
case "400":
(rdr as ADODB.Recordset).MoveNext();
eof = (rdr as ADODB.Recordset).EOF;
break;
default:
eof = !(rdr as SqlDataReader).Read();
break;
}
}
public void Close()
{
if (rdr != null)
switch (conntype)
{
case "400":
//(rdr as ADODB.Recordset).Close();
//(conn as JCT.FrameWork.DataAccess.AS400).cn.Close();
if ((conn as ADODB.Connection).State == 1)
(conn as ADODB.Connection).Close();
break;
default:
if (!(rdr as SqlDataReader).IsClosed)
(rdr as SqlDataReader).Close();
rdr = null;
cmd = null;
break;
}
eof = true;
inUse = false;
fieldCount = 0;
}
public String stringValue(int i)
{
String result = "";
switch (conntype)
{
case "400":
result = (rdr as ADODB.Recordset).Fields[i].Value.ToString().Trim();
break;
default:
result = (rdr as SqlDataReader)[i].ToString().Trim();
break;
}
return result;
}
public String stringValue(String fieldname)
{
String result = "";
switch (conntype)
{
case "400":
result = (rdr as ADODB.Recordset).Fields[fieldname].Value.ToString().Trim();
break;
default:
result = (rdr as SqlDataReader)[fieldname].ToString().Trim();
break;
}
return result;
}
public object Value(int i)
{
object result = "";
switch (conntype)
{
case "400":
result = (rdr as ADODB.Recordset).Fields[i].Value;
break;
default:
result = (rdr as SqlDataReader)[i];
break;
}
return result;
}
public object Value(String fieldname)
{
object result = "";
switch (conntype)
{
case "400":
result = (rdr as ADODB.Recordset).Fields[fieldname].Value;
break;
default:
result = (rdr as SqlDataReader)[fieldname];
break;
}
return result;
}
public String fieldName(int i)
{
String result = "";
switch (conntype)
{
case "400":
result = (rdr as ADODB.Recordset).Fields[i].Name;
break;
default:
result = (rdr as SqlDataReader).GetName(i);
break;
}
return result;
}
}
public class DBManager
{
private List<DBConnection> DBConnections = new List<DBConnection>();
~DBManager()
{
destroyAllConns();
}
public void destroyAllConns()
{
foreach (DBConnection conn in DBConnections)
{
if (conn.inUse)
try
{
conn.Close();
}
catch { }
}
}
public DBConnection getConnObject(String ctype = "400")
{
ctype = ctype.ToUpper();
//go through connections and find one of the appropriate type that is not in use
foreach (DBConnection conn in DBConnections)
{
if (conn.conntype == ctype && !conn.inUse)
{
return conn;
}
}
DBConnection newconn = new DBConnection(ctype);
DBConnections.Add(newconn);
//if we can't find one, then allocate one and add it to the dbConns list
return newconn;
}
public String parmClean(String x)
{
return x.Replace("''","'").Replace("'", "''");
}
}
}