Oracle数据库操作类(c#)
2024-07-21 02:18:40
供稿:网友
用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();
}
}