Skip to content

Commit f201e71

Browse files
authored
Add files via upload
1 parent 74ced2b commit f201e71

File tree

3 files changed

+363
-63
lines changed

3 files changed

+363
-63
lines changed

Loger.cs

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,58 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.IO;
6-
using System.Reflection;
7-
using System.Windows.Forms;
8-
using System.Web;
9-
10-
namespace HFSIFrameLib.COM
11-
{
12-
public class Loger
13-
{
14-
15-
public static void WriteLog(string Message)
16-
{
17-
if (System.Configuration.ConfigurationManager.AppSettings["Log"] != null
18-
&& "TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper()))
19-
{
20-
string filePath = Path.Combine(Application.StartupPath, "Logs");
21-
if (HttpContext.Current != null && HttpContext.Current.Server != null && Directory.Exists(HttpContext.Current.Server.MapPath("/")))
22-
{
23-
filePath = Path.Combine(HttpContext.Current.Server.MapPath("/"), "Logs");
24-
}
25-
if (!Directory.Exists(filePath))
26-
{
27-
Directory.CreateDirectory(filePath);
28-
}
29-
List<string> list = new List<string>();
30-
list.Add(string.Format("时间:{0}----------------------------------------------------------------------", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
31-
list.Add(Message);
32-
33-
try
34-
{
35-
if (!Directory.Exists(filePath))
36-
{
37-
Directory.CreateDirectory(filePath);
38-
}
39-
40-
File.AppendAllLines(Path.Combine(filePath, DateTime.Now.ToString("yyyy-MM-dd-HH") + ".log"), list, Encoding.Default);
41-
}
42-
catch
43-
{ }
44-
}
45-
}
46-
47-
public static void WriteLog(Exception ex)
48-
{
49-
if (System.Configuration.ConfigurationManager.AppSettings["Log"] == null
50-
|| !"TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper()))
51-
{
52-
return;
53-
}
54-
55-
StringBuilder sb = new StringBuilder();
56-
sb.AppendLine(string.Format("[ERRORMESSAGE]:{0}", ex.Message));
57-
sb.AppendLine(string.Format("[INNEREXCEPTION]:{0}", ex.InnerException));
58-
sb.AppendLine(string.Format("[SOURCE]:{0}", ex.Source));
59-
sb.AppendLine(string.Format("[STACKTRACE]:{0}", ex.StackTrace));
60-
WriteLog(sb.ToString());
61-
}
62-
}
63-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
using System.Reflection;
7+
using System.Windows.Forms;
8+
9+
namespace DataSync
10+
{
11+
public class Loger
12+
{
13+
14+
public static void WriteLog(string Message)
15+
{
16+
if (System.Configuration.ConfigurationManager.AppSettings["Log"] != null
17+
&& "TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper()))
18+
{
19+
string filePath = Path.Combine(Application.StartupPath, "Logs");
20+
if (!Directory.Exists(filePath))
21+
{
22+
Directory.CreateDirectory(filePath);
23+
}
24+
List<string> list = new List<string>();
25+
list.Add(string.Format("时间:{0}----------------------------------------------------------------------", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
26+
list.Add(Message);
27+
28+
try
29+
{
30+
if (!Directory.Exists(filePath))
31+
{
32+
Directory.CreateDirectory(filePath);
33+
}
34+
35+
File.AppendAllLines(Path.Combine(filePath, DateTime.Now.ToString("yyyy-MM-dd") + ".log"), list, Encoding.Default);
36+
}
37+
catch
38+
{ }
39+
}
40+
}
41+
42+
public static void WriteLog(Exception ex)
43+
{
44+
if (System.Configuration.ConfigurationManager.AppSettings["Log"] == null
45+
|| !"TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper()))
46+
{
47+
return;
48+
}
49+
50+
StringBuilder sb = new StringBuilder();
51+
sb.AppendLine(string.Format("[ERRORMESSAGE]:{0}", ex.Message));
52+
sb.AppendLine(string.Format("[INNEREXCEPTION]:{0}", ex.InnerException));
53+
sb.AppendLine(string.Format("[SOURCE]:{0}", ex.Source));
54+
sb.AppendLine(string.Format("[STACKTRACE]:{0}", ex.StackTrace));
55+
WriteLog(sb.ToString());
56+
}
57+
}
58+
}

OledbHelper.cs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Data.OleDb;
5+
using System.Data;
6+
using System.Threading;
7+
using DataSync;
8+
9+
namespace DataSync
10+
{
11+
public static class OledbHelper
12+
{
13+
private static OleDbConnection conn;
14+
static OledbHelper()
15+
{
16+
if (conn == null)
17+
{
18+
conn = new OleDbConnection();
19+
conn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["AccessStr"].ToString();
20+
}
21+
}
22+
23+
24+
/// <summary>
25+
/// 查询返回DataTable
26+
/// </summary>
27+
/// <param name="sql">SQL语句</param>
28+
/// <param name="parameters">参数列表</param>
29+
/// <returns></returns>
30+
public static DataTable GetDataTable(string sql, OleDbParameter[] parameters = null)
31+
{
32+
try
33+
{
34+
lock (conn)
35+
{
36+
DataTable dt = new DataTable();
37+
OleDbCommand oleCmd = new OleDbCommand();
38+
oleCmd.CommandText = sql;
39+
if (parameters != null && parameters.Length > 0)
40+
{
41+
oleCmd.Parameters.AddRange(parameters);
42+
}
43+
oleCmd.Connection = conn;
44+
OleDbDataAdapter da = new OleDbDataAdapter(oleCmd);
45+
da.Fill(dt);
46+
47+
return dt;
48+
}
49+
}
50+
catch (Exception ex)
51+
{
52+
Loger.WriteLog(ex);
53+
return null;
54+
}
55+
}
56+
57+
/// <summary>
58+
/// 执行SQL语句
59+
/// </summary>
60+
/// <param name="sql">SQL语句</param>
61+
/// <param name="parameters">参数列表</param>
62+
/// <returns></returns>
63+
public static bool ExecSql(string sql, OleDbParameter[] parameters = null)
64+
{
65+
try
66+
{
67+
lock (conn)
68+
{
69+
conn.Open();
70+
OleDbCommand oleCmd = new OleDbCommand();
71+
oleCmd.CommandText = sql;
72+
if (parameters != null && parameters.Length > 0)
73+
{
74+
oleCmd.Parameters.AddRange(parameters);
75+
}
76+
77+
oleCmd.Connection = conn;
78+
if (oleCmd.ExecuteNonQuery() > 0)
79+
{
80+
return true;
81+
}
82+
else
83+
{
84+
return false;
85+
}
86+
}
87+
}
88+
catch (Exception ex)
89+
{
90+
Loger.WriteLog(ex);
91+
return false;
92+
}
93+
finally
94+
{
95+
conn.Close();
96+
}
97+
}
98+
99+
/// <summary>
100+
/// 以事务方式执行SQL语句
101+
/// </summary>
102+
/// <param name="listsqls">SQL语句list</param>
103+
/// <param name="listparameters">参数列表list</param>
104+
/// <returns>成功返回true 否则返回false</returns>
105+
public static bool ExecSqlByTran(List<string> listsqls, List<OleDbParameter[]> listparameters = null)
106+
{
107+
if (listsqls == null || listparameters == null || listsqls.Count <= 0 || listparameters.Count <= 0 || listsqls.Count != listparameters.Count)
108+
{
109+
return false;
110+
}
111+
else
112+
{
113+
lock (conn)
114+
{
115+
conn.Open();
116+
OleDbTransaction tran = conn.BeginTransaction();
117+
try
118+
{
119+
for (int i = 0; i < listsqls.Count; i++)
120+
{
121+
OleDbCommand oleCmd = new OleDbCommand();
122+
oleCmd.CommandText = listsqls[i];
123+
if (listparameters != null)
124+
{
125+
oleCmd.Parameters.AddRange(listparameters[i]);
126+
}
127+
oleCmd.Connection = conn;
128+
oleCmd.Transaction = tran;
129+
oleCmd.ExecuteNonQuery();
130+
}
131+
tran.Commit();
132+
conn.Close();
133+
return true;
134+
}
135+
catch (Exception ex)
136+
{
137+
tran.Rollback();
138+
conn.Close();
139+
Loger.WriteLog(ex);
140+
return false;
141+
}
142+
}
143+
}
144+
}
145+
146+
}
147+
}

0 commit comments

Comments
 (0)