C#--Oracle数据库基本操作(增、删、改、查)
写在前面:
常用数据库:
SQLserver:https://www.cnblogs.com/mexihq/p/11636785.html
Oracle:https://www.cnblogs.com/mexihq/p/11700741.html
MySQL:https://www.cnblogs.com/mexihq/p/12463423.html
Access:https://www.cnblogs.com/mexihq/p/12466970.html
类似于上篇有关SQLserver的C#封装,小编对Oracle数据库进行了相应的封装,方便后期开发使用,主要包括Oracle数据库的连接、增、删、改、查,如有什么问题还请各位大佬指教。后续也将对其他几个常用的数据库进行相应的整理。话不多说,直接开始码代码。
引用:
由于微软在.Net框架4.0中已经决定撤销使用System.Data.OracleClient,造成在VS中无法连接Oracle数据库,但它还依旧存在于.Net架构中,我们可以通过自己引用。具体方法如下:
(1)在需要引用的程序集引用文件夹上右击,选择添加引用
(2)选择浏览选项
(3)找到目录 C:\Windows\Microsoft.NET\Framework\v2.0.50727
(4)找到 System.Data.OracleClient.dll 文件
(5)点击确定。OK,引用完成
using System.Data; //DataSet引用集 using System.Data.OracleClient; //oracle引用
先声明一个SqlConnection
private OracleConnection oracle_con;//声明一个OracleConnection方便使用
Oracle打开:
/// <summary>
/// Oracle open
/// </summary>
/// <param name="link">link statement</param>
/// <returns>Success:success; Fail:reason</returns>
public string Oracle_Open(string link)
{
try
{
oracle_con = new OracleConnection(link);
oracle_con.Open();
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
Oracle关闭:
/// <summary>
/// Oracle close
/// </summary>
/// <returns>Success:success Fail:reason</returns>
public string Oracle_Close()
{
try
{
if (oracle_con == null)
{
return "No database connection";
}
if (oracle_con.State == ConnectionState.Open)
{
oracle_con.Close();
oracle_con.Dispose();
}
else
{
if (oracle_con.State == ConnectionState.Closed)
{
return "success";
}
if (oracle_con.State == ConnectionState.Broken)
{
return "ConnectionState:Broken";
}
}
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
Oracle的增删改:
/// <summary>
/// Oracle insert,delete,update
/// </summary>
/// <param name="sql">insert,delete,update statement</param>
/// <returns>Success:success + Number of affected rows; Fail:reason</returns>
public string Oracle_Insdelupd(string sql)
{
try
{
int num = 0;
if (oracle_con == null)
{
return "Please open the database connection first";
}
if (oracle_con.State == ConnectionState.Open)
{
OracleCommand oracleCommand = new OracleCommand(sql, oracle_con);
num = oracleCommand.ExecuteNonQuery();
}
else
{
if (oracle_con.State == ConnectionState.Closed)
{
return "Database connection closed";
}
if (oracle_con.State == ConnectionState.Broken)
{
return "Database connection is destroyed";
}
}
return "success" + num;
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
Oracle的查:
/// <summary>
/// Oracle select
/// </summary>
/// <param name="sql">select statement</param>
/// <param name="record">Success:success; Fail:reason</param>
/// <returns>select result</returns>
public DataSet Oracle_Select(string sql, out string record)
{
try
{
DataSet dataSet = new DataSet();
if (oracle_con != null)
{
if (oracle_con.State == ConnectionState.Open)
{
OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(sql, oracle_con);
oracleDataAdapter.Fill(dataSet, "sample");
oracleDataAdapter.Dispose();
record = "OK";
return dataSet;
}
if (oracle_con.State == ConnectionState.Closed)
{
record = "Database connection closed";
}
else if (oracle_con.State == ConnectionState.Broken)
{
record = "Database connection is destroyed";
}
}
else
{
record = "Please open the database connection first";
}
record = "error";
return dataSet;
}
catch (Exception ex)
{
DataSet dataSet = new DataSet();
record = ex.Message.ToString();
return dataSet;
}
}
小编发现以上这种封装方式还是很麻烦,每次对Oracle进行增删改查的时候还得先打开数据库,最后还要关闭,实际运用起来比较麻烦。因此对上面两个增删改查的方法进行了重载,在每次进行操作时都先打开数据库,然后关闭数据库。
/// <summary>
/// Oracle insert,delete,update
/// </summary>
/// <param name="sql">insert,delete,update statement</param>
/// <param name="link">link statement</param>
/// <returns>Success:success + Number of affected rows; Fail:reason</returns>
public string Oracle_Insdelupd(string sql, string link)
{
try
{
int num = 0;
using (OracleConnection oracleConnection = new OracleConnection(link))
{
DataSet dataSet = new DataSet();
oracleConnection.Open();
OracleCommand oracleCommand = new OracleCommand(sql, oracleConnection);
num = oracleCommand.ExecuteNonQuery();
oracleConnection.Close();
return "success" + num;
}
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
/// <summary>
/// Oracle select
/// </summary>
/// <param name="sql">select statement</param>
/// <param name="link">link statement</param>
/// <param name="record">Success:success; Fail:reason</param>
/// <returns>select result</returns>
public DataSet Oracle_Select(string sql, string link, out string record)
{
try
{
using (OracleConnection oracleConnection = new OracleConnection(link))
{
DataSet dataSet = new DataSet();
oracleConnection.Open();
OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(sql, oracleConnection);
oracleDataAdapter.Fill(dataSet, "sample");
oracleDataAdapter.Dispose();
oracleConnection.Close();
record = "success";
return dataSet;
}
}
catch (Exception ex)
{
DataSet dataSet = new DataSet();
record = ex.Message.ToString();
return dataSet;
}
}