#region 声明
//———————————————————————-
//
// 修改: 李淼(nick.lee)
//
// oracle9i连接asp.net方法及注意点
// 时间:2005-3-18
// boyorgril@msn.com
// qq:16503096
//注意:引用请标明修改出处,谢谢
//———————————————————————-
#endregion
推荐方式
public void oledboracle_dataset()
{
string connectionstring="data source=mine;user=sys;password=sys;"; //写连接串
oracleconnection conn=new oracleconnection(connectionstring); //创建一个新连接
oraclecommand cmd= new oraclecommand("select * from fjdl.t_rights",conn);
dataset ds = new dataset();
oracledataadapter oda=new oracledataadapter();
oda.selectcommand=cmd;
oda.fill(ds);
conn.close();
datagrid1.datasource=ds.tables[0].defaultview;
datagrid1.databind();
}
以下为用dataset和datareader,oledb和oralceclient的四种方法
public void oledboracle_datareader()
{
system.data.oledb.oledbconnection oledb1=new system.data.oledb.oledbconnection();
string sqltext="select * from scott.tadmin";
system.data.oledb.oledbdatareader reader;
oledb1.connectionstring="provider=\"oraoledb.oracle.1\";user id=system;password=system;data source=rick;";
system.data.oledb.oledbcommand com=new system.data.oledb.oledbcommand(sqltext,oledb1);
com.connection.open();
reader=com.executereader();
datagrid1.datasource=reader;
datagrid1.databind();
//this.response.write(oledbconnection1.connectionstring);
reader.close();
com.connection.close();
}
public void oledboracle_dataset()
{
system.data.oledb.oledbconnection oledb1=new system.data.oledb.oledbconnection("provider=\"oraoledb.oracle.1\";user id=system;password=system;data source=rick;");
string sqltext="select * from scott.tadmin";
system.data.dataset set1=new dataset();
system.data.oledb.oledbdataadapter ada1=new system.data.oledb.oledbdataadapter (sqltext,oledb1);
ada1.fill(set1,"data1");
this.datagrid2.datasource=set1.tables["data1"].defaultview;
this.datagrid2.databind();
}
public void oracle_dataset()
{
system.data.oracleclient.oracleconnection oracle1=new system.data.oracleclient.oracleconnection("user id=system;data source=rick;password=system");
string sqltext="select * from scott.tadmin";
system.data.dataset set1=new dataset();
system.data.oracleclient.oracledataadapter ada1=new system.data.oracleclient.oracledataadapter(sqltext,oracle1);
ada1.fill(set1,"oracle_data1");
this.datagrid3.datasource=set1.tables["oracle_data1"].defaultview;
this.datagrid3.databind();
}
public void oracle_datareader()
{
system.data.oracleclient.oracleconnection oracle2=new system.data.oracleclient.oracleconnection();
string sqltext="select * from scott.tadmin";
system.data.oracleclient.oracledatareader reader;
oracle2.connectionstring="user id=system;user id=system;data source=rick;password=system";
system.data.oracleclient.oraclecommand com=new system.data.oracleclient.oraclecommand(sqltext,oracle2);
com.connection.open();
reader=com.executereader();
datagrid4.datasource=reader;
datagrid4.databind();
//this.response.write(oledbconnection1.connectionstring);
reader.close();
com.connection.close();
}
注意点:
如果不设置远程数据的本级网络配置
ora-00162: external dbid length 18 is greater than maximum (16)
连接名称不能超过16个字符
修改方法
configuration and migration tools->net configuration assistant中配置