C#--Oracle数据库基本操作(连接、增、删、改、查)封装

 写在前面:

 SQLserver的C#封装:https://www.cnblogs.com/mexihq/p/11636785.html

 Oracle的C#封装:https://www.cnblogs.com/mexihq/p/11700741.html

 MySQL的C#封装:https://www.cnblogs.com/mexihq/p/12463423.html

 Access的C#封装:https://www.cnblogs.com/mexihq/p/12466970.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;
  }
}

 

发布了2 篇原创文章 · 获赞 0 · 访问量 39
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览