程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> Oracle數據庫操作類(c#)

Oracle數據庫操作類(c#)

編輯:.NET實例教程

用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();
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved