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