当前位置:网站首页>C#使用Oracle.ManagedDataAccess连接C#数据库
C#使用Oracle.ManagedDataAccess连接C#数据库
2022-08-03 05:27:00 【谷凯Jump】
使用NuGet安装Oracle.ManagedDataAccess
- 有个小问题,安装后,vs会自动在app.config添加这些内容,需要将下图红色框中注释掉,否则在使用ConfigurationManager.ConnectionStrings["DemoConnString"]会报错 -_-! 此坑今天困扰我3小时....
添加连字符串
<connectionStrings> <add name="DemoConnString" connectionString="User Id=wpfdemo;Password=wpfdemo;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.10.10.2)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)))" providerName="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess"/> </connectionStrings>
添加OracleHelper类
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helper
{
/// <summary>
/// Oracle数据库操作类
/// </summary>
public static class OracleHelper
{
//从配置文件中读取配置好的连接字符串
public static readonly string ConnectionString_Default = ConfigurationManager.ConnectionStrings["DemoConnString"].ConnectionString;
//简易SQL, by gukai
public static string ExecuteSQL(string SQLString)
{
try
{
ExecuteNonQuery(ConnectionString_Default, SQLString);
return "success";
}
catch (Exception ex)
{
return "fail." + ex.Message;
}
}
public static string ExecuteSQL(ArrayList SQLStringArrayList)
{
try
{
ExecuteNonQuery(ConnectionString_Default, SQLStringArrayList);
return "success";
}
catch (Exception ex)
{
return "fail." + ex.Message;
}
}
public static string ExecuteSQL(List<string> SQLStringList)
{
try
{
ExecuteNonQuery(ConnectionString_Default, SQLStringList);
return "success";
}
catch (Exception ex)
{
return "fail." + ex.Message;
}
}
public static int ExecuteNonQuery(string SQLString)
{
return ExecuteNonQuery(ConnectionString_Default, SQLString);
}
public static void ExecuteNonQuery(ArrayList SQLStringList)
{
ExecuteNonQuery(ConnectionString_Default, SQLStringList);
}
public static DataTable ExecuteDataTable(string SQLString)
{
return ExecuteDataTable(ConnectionString_Default, CommandType.Text, SQLString, null);
}
public static object ExecuteScalar(string SQLString)
{
return ExecuteScalar(ConnectionString_Default, CommandType.Text, SQLString, null);
}
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
private static int ExecuteNonQuery(string connectionString, string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
connection.Close();
return rows;
}
catch (OracleException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
private static void ExecuteNonQuery(string connectionString, ArrayList SQLStringArrayList)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringArrayList.Count; n++)
{
string strsql = SQLStringArrayList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (OracleException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
private static void ExecuteNonQuery(string connectionString, List<string> SQLStringList)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (OracleException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
/// <summary>
/// 执行数据库非查询操作,返回受影响的行数
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前操作影响的数据行数</returns>
private static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
using (OracleCommand cmd = new OracleCommand())
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
}
/// <summary>
/// 执行数据库事务非查询操作,返回受影响的行数
/// </summary>
/// <param name="transaction">数据库事务对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前事务操作影响的数据行数</returns>
private static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 执行数据库非查询操作,返回受影响的行数
/// </summary>
/// <param name="connection">Oracle数据库连接对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前操作影响的数据行数</returns>
private static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
if (connection == null)
throw new ArgumentNullException("当前数据库连接不存在");
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 执行数据库查询操作,返回OracleDataReader类型的内存结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的OracleDataReader类型的内存结果集</returns>
private static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch
{
cmd.Dispose();
conn.Close();
throw;
}
}
/// <summary>
/// 执行数据库查询操作,返回DataSet类型的结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的DataSet类型的结果集</returns>
private static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
DataSet ds = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = cmd;
ds = new DataSet();
adapter.Fill(ds);
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return ds;
}
/// <summary>
/// 执行数据库查询操作,返回DataTable类型的结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的DataTable类型的结果集</returns>
private static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
DataTable dt = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = cmd;
dt = new DataTable();
adapter.Fill(dt);
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return dt;
}
/// <summary>
/// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
private static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
object result = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 执行数据库事务查询操作,返回结果集中位于第一行第一列的Object类型的值
/// </summary>
/// <param name="trans">一个已存在的数据库事务对象</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前事务查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
private static object ExecuteScalar(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
if (trans == null)
throw new ArgumentNullException("当前数据库事务不存在");
OracleConnection conn = trans.Connection;
if (conn == null)
throw new ArgumentException("当前事务所在的数据库连接不存在");
OracleCommand cmd = new OracleCommand();
object result = null;
try
{
PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
trans.Dispose();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值
/// </summary>
/// <param name="conn">数据库连接对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
private static object ExecuteScalar(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
if (conn == null) throw new ArgumentException("当前数据库连接不存在");
OracleCommand cmd = new OracleCommand();
object result = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 执行数据库命令前的准备工作
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">数据库连接对象</param>
/// <param name="trans">事务对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 将.NET日期时间类型转化为Oracle兼容的日期时间格式字符串
/// </summary>
/// <param name="date">.NET日期时间类型对象</param>
/// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>
private static string GetOracleDateFormat(DateTime date)
{
return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','YYYY-MM-DD')";
}
/// <summary>
/// 将.NET日期时间类型转化为Oracle兼容的日期格式字符串
/// </summary>
/// <param name="date">.NET日期时间类型对象</param>
/// <param name="format">Oracle日期时间类型格式化限定符</param>
/// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>
private static string GetOracleDateFormat(DateTime date, string format)
{
if (format == null || format.Trim() == "") format = "YYYY-MM-DD";
return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','" + format + "')";
}
/// <summary>
/// 将指定的关键字处理为模糊查询时的合法参数值
/// </summary>
/// <param name="source">待处理的查询关键字</param>
/// <returns>过滤后的查询关键字</returns>
private static string HandleLikeKey(string source)
{
if (source == null || source.Trim() == "") return null;
source = source.Replace("[", "[]]");
source = source.Replace("_", "[_]");
source = source.Replace("%", "[%]");
return ("%" + source + "%");
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="connection">SqlServer数据库连接对象</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlDataReader对象</returns>
private static OracleDataReader RunStoredProcedure(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
{
OracleDataReader returnReader = null;
connection.Open();
OracleCommand command = BuildSqlCommand(connection, storedProcName, parameters);
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
/// <summary>
/// 构建SqlCommand对象
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private static OracleCommand BuildSqlCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
{
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
}
}
添加System.Configuration引用
调用
DataTable dt =Helper.OracleHelper.ExecuteDataTable("Select * From Dual");
边栏推荐
- MySql的Sql语句的练习(试试你能写出来几道呢)
- Eight, the difference between the interface of the abstract class
- 使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
- Prometheus监控容器、pod、邮件告警
- 802.1AS的BMCA(最佳主时钟选举)理解
- contos安装php-ffmpeg和tp5.1使用插件
- Unity Animation从UAS获取动画资产到编制状态机控制简单的人物动画
- AI智能剪辑,仅需2秒一键提取精彩片段
- 借助ginput函数在figure窗口实时读取、展示多条曲线的坐标值
- SQLMAP介绍及使用
猜你喜欢
随机推荐
3D建模为什么会变得无处不在
ZEMAX | 在设计抬头显示器(HUD)时需要使用哪些工具?
交换机access口,hybrid口,trunk口的区别
servlet学习(七)ServletContext
9. Please introduce the class loading process, what is the parent delegation model?
电子元器件和电子元件的区别有那些?
【面筋1】一些没什么标准答案的问题
MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测
剑指 Offer II 001. 整数除法
ZEMAX | 探究 OpticStudio 偏振分析功能
JSP的基本使用
C语言中打印字符数组出现乱码的问题(烫烫烫)
关于C语言中getchar()函数的用法
常见的电子元器件分类介绍
ZBrush+Substance Designer2021制作高品质3D角色模型全流程!
SSL证书验证怎么做?有几种方式?
【C语言】二分查找
Dynamic adjustment of web theme (2) Extraction
2021-06-14
二、Exception和Error有什么区别?