-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMySQLHelper.cs
More file actions
270 lines (257 loc) · 8.17 KB
/
Copy pathMySQLHelper.cs
File metadata and controls
270 lines (257 loc) · 8.17 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
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
using BaseFun;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace MySQLHelper
{
public static class MySQLHelper
{
public static string connectionString { get; set; }
public static MySqlConnection conn()
{
try
{
if (connectionString == null)
{
connectionString = appSittingSet.readAppsettings("MySqlConnect").Split('|')[0];
}
MySqlConnection connection = new MySqlConnection(connectionString);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
return connection;
}
catch (MySqlException ex)
{
appSittingSet.Log(ex.Message+ex.ToString());
throw;
}
}
/// <summary>
/// 判断链接状态
/// </summary>
/// <returns></returns>
public static bool IsOpen()
{
return conn().State == ConnectionState.Open;
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
MySqlConnection connection = conn();
DataSet ds = new DataSet();
try
{
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds);
}
catch (SqlException ex)
{
//throw new Exception(ex.Message);
appSittingSet.Log(ex.Message + ex.ToString());
}
finally
{
connection.Close();
}
return ds;
}
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
return ExecuteSql(SQLString, null);
}
public static int ExecuteSql(string SQLString, List<MySqlParameter> para)
{
MySqlConnection connection = conn();
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
if (para != null && para.Count > 0)
{
cmd.Parameters.AddRange(para.ToArray());
}
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (SqlException ex)
{
connection.Close();
appSittingSet.Log(ex.Message + ex.ToString());
throw;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
//执行多条SQL语句,实现数据库事务。
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static bool ExecuteNoQueryTran(List<String> SQLStringList)
{
MySqlConnection connection = conn();
MySqlCommand cmd = new MySqlCommand();
MySqlTransaction tx = connection.BeginTransaction();
cmd.Connection = connection;
cmd.Transaction = tx;
try
{
foreach (var item in SQLStringList)
{
cmd.CommandText = item;
cmd.ExecuteNonQuery();
}
tx.Commit();
return true;
}
catch(Exception ex)
{
tx.Rollback();
appSittingSet.Log(ex.Message + ex.ToString());
throw;
}
}
/// <summary>
/// 执行SQL语句数组,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string[] arrSql)
{
MySqlConnection connection = conn();
try
{
//MySqlCommand cmdEncoding = new MySqlCommand(SET_ENCODING, connection);
//cmdEncoding.ExecuteNonQuery();
int rows = 0;
foreach (string strN in arrSql)
{
using (MySqlCommand cmd = new MySqlCommand(strN, connection))
{
rows += cmd.ExecuteNonQuery();
}
}
return rows;
}
catch (SqlException ex)
{
connection.Close();
appSittingSet.Log(ex.Message + ex.ToString());
throw;
}
finally
{
connection.Close();
}
}
/// <summary>
/// 是否存在记录
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static bool Exsist(string SQLString)
{
MySqlConnection connection = conn();
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
MySqlDataReader sdr = cmd.ExecuteReader();
return sdr.HasRows;
}
catch (SqlException ex)
{
connection.Close();
appSittingSet.Log(ex.Message + ex.ToString());
throw;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 获取符合条件的记录行数
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static int GetCount(string SQLString)
{
//object o = GetScalar(SQLString);
//return o == null ? 0 : (int)o;
MySqlConnection connection = conn();
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
int i = 0;
MySqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
i++;
}
sdr.Close();
return i;
}
catch (SqlException ex)
{
connection.Close();
appSittingSet.Log(ex.Message + ex.ToString());
throw;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 获取符合条件的首行首列
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static object GetScalar(string SQLString)
{
try
{
MySqlConnection connection = conn();
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
object o = cmd.ExecuteScalar();
return o;
}
}
catch (SqlException ex)
{
appSittingSet.Log(ex.Message + ex.ToString());
return null;
}
}
public static T GetScalar<T>(string SQLString)
{
object o = GetScalar(SQLString);
return o != null ? (T)Convert.ChangeType(o, typeof(T)) : default(T);
}
}
}