Oracle数据库操作类(c#)

类别:.NET开发 点击:0 评论:0 推荐:

用OracleConnection连接Oracle
在Web.config中配置 <add key="Oracle" value="Data Source=xxx;User ID=xxx;Password=xxx"/>

  public System.Data.OracleClient.OracleConnection cnn;

  /// <summary>
  /// 打开数据库连接
  /// </summary>
  public void Open()
  {
   cnn=new OracleConnection(System.Configuration.ConfigurationSettings.AppSettings["Oracle"]);
   cnn.Open();
  }

  /// <summary>
  /// 打开数据库连接,返回cnn
  /// </summary>
  public OracleConnection OpenCnn()
  {
   cnn=new OracleConnection(System.Configuration.ConfigurationSettings.AppSettings["Oracle"]);
   cnn.Open();
   return(cnn);
  }

  /// <summary>
  /// 关闭数据库连接
  /// </summary>
  public void Close()
  {
   cnn.Close();
  }

  /// <summary>
  /// 返回DataSet
  /// </summary>
  /// <param name="CmdString"></param>
  /// <param name="TableName"></param>
  /// <returns></returns>
  public DataSet GetDataSet(string CmdString,string TableName)
  {
   Open();
   OracleDataAdapter myDa =new OracleDataAdapter();
   myDa.SelectCommand = new OracleCommand(CmdString,cnn);
   DataSet myDs =new DataSet();
   myDa.Fill(myDs,TableName);
   Close();
   return myDs;
  }

  /// <summary>
  /// 返回OleDbDataReader
  /// </summary>
  /// <param name="CmdString"></param>
  /// <returns></returns>
  public OracleDataReader GetDataReader(string CmdString)
  {
   Open();
   OracleCommand myCmd =new OracleCommand(CmdString,cnn);
   OracleDataReader myDr =myCmd.ExecuteReader();
   return myDr;
  }

  /// <summary>
  /// 返回影响数据库的行数
  /// </summary>
  /// <param name="CmdString"></param>
  /// <returns></returns>
  public int ExecuteSQL(string CmdString)
  {
   Open();
   OracleCommand myCmd =new OracleCommand(CmdString,cnn);
   int Cmd =myCmd.ExecuteNonQuery();
   Close();
   return Cmd;
  }

  /// <summary>
  /// 邦定DroDownList
  /// </summary>
  /// <param name="DroDList"></param>控件名
  /// <param name="sql"></param>连接字符串
  /// <param name="TableName"></param>表名
  /// <param name="DataTextFd"></param>提供文本内容的数据源字段
  /// <param name="DataValueFd"></param>为列表项提供值的数据源字段
  public void  DroList(DropDownList DroDList,string sql,string TableName,string DataTextFd,string DataValueFd)
  {
   Open();
   OracleDataAdapter myDa =new OracleDataAdapter(sql,cnn);
   myDa.SelectCommand.CommandType = CommandType.Text;
   DataSet myDs =new DataSet();
   try
   {
    myDa.Fill(myDs,TableName);
    DroDList.DataSource = myDs.Tables[TableName];
    DroDList.DataTextField = DataTextFd;
    DroDList.DataValueField = DataValueFd;
    DroDList.DataBind();
   }
   catch(Exception e)
   {
    Response.Write(e.Message);
   }
   finally
   {
    Close();
   }
  }

本文地址:http://com.8s8s.com/it/it43015.htm