日期:2008-09-09  浏览次数:20479 次

#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中配置