C#教程之C#学习--Oracle数据库基本操作(连接、增

金沙澳门官网网址:www.js333com

VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之C#学习--Oracle数据库基本操作(连接、增

  • 2019-10-19 09:50 来源:www.js333com

写在前面:

 SQLserver的C#封装:http://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;
  }
}

复制代码

 

小编只是对简单的知识进行整理,通大家分享并方便自己查看,并无恶意,如有侵权,联系小编,小编将立即删除,谢谢。
相关教程
Baidu
sogou