寫在前面: SQLserver的C#封裝:https://www.cnblogs.com/mexihq/p/11636785.html 類似於上篇有關SQLserver的C#封裝,小編對Oracle資料庫進行了相應的封裝,方便後期開發使用,主要包括Oracle資料庫的連接、增、刪、改、查,如有什麼問 ...
寫在前面:
SQLserver的C#封裝:https://www.cnblogs.com/mexihq/p/11636785.html
類似於上篇有關SQLserver的C#封裝,小編對Oracle資料庫進行了相應的封裝,方便後期開發使用,主要包括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;
}
}