主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:207次
用OracleConnection连接Oracle
在Web.config中配置

public System.Data.OracleClient.OracleConnection cnn;

///
/// 打开数据库连接
///

public void Open()
{
cnn=new OracleConnection(System.Configuration.ConfigurationSettings.AppSettings["Oracle"]);
cnn.Open();
}

///
/// 打开数据库连接,返回cnn
///

public OracleConnection OpenCnn()
{
cnn=new OracleConnection(System.Configuration.ConfigurationSettings.AppSettings["Oracle"]);
cnn.Open();
return(cnn);
}

///
/// 关闭数据库连接
///

public void Close()
{
cnn.Close();
}

///
/// 返回DataSet
///

///
///
///
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;
}

///
/// 返回OleDbDataReader
///

///
///
public OracleDataReader GetDataReader(string CmdString)
{
Open();
OracleCommand myCmd =new OracleCommand(CmdString,cnn);
OracleDataReader myDr =myCmd.ExecuteReader();
return myDr;
}

///
/// 返回影响数据库的行数
///

///
///
public int ExecuteSQL(string CmdString)
{
Open();
OracleCommand myCmd =new OracleCommand(CmdString,cnn);
int Cmd =myCmd.ExecuteNonQuery();
Close();
return Cmd;
}

///
/// 邦定DroDownList
///

/// 控件名
/// 连接字符串
/// 表名
/// 提供文本内容的数据源字段
/// 为列表项提供值的数据源字段
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();
}
}



关键字词: