Skip to content

Commit 1dee2a8

Browse files
authored
Create OraHelper_OracleManagedDataAccess.cs
1 parent cda8ba3 commit 1dee2a8

File tree

1 file changed

+140
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)