1.安裝ORACLE用戶端9,修改C:\Oracle\ora90\network\ADMIN下的tnsnames.ora檔案為:
# TNSNAMES.ORA Network Configuration File: C:\oracle\ora90\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.
dpas =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.70)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ORCL)
)
) 2.在方案總管中,右擊引用->添加引用->瀏覽->選擇System.Data.OracleClient.dll->確定,然後using System.Data.OracleClient。 下載 3.在web.config中加入:<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=dpas;User ID=dpas;Password=dpas;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings> 4. 後台代碼:
using System.Data.OracleClient; //要引用的類庫
public partial class Demo3 : System.Web.UI.Page
{
public System.Data.OracleClient.OracleConnection objConn; //聲明一個OracleConnection對象
public System.Data.OracleClient.OracleCommand objCmd; //聲明一個OracleCommand對象
public System.Data.OracleClient.OracleDataAdapter objAdp; //資料庫適配器
public System.Data.DataSet dset; //DataSet 用於資料填充
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
objConn = new OracleConnection(strconn);
}
protected void Btn_Search_Click(object sender, EventArgs e)
{
try
{
string StrSql = "select * from DPA";
objConn.Open();
objCmd = new OracleCommand(StrSql, objConn);
objAdp = new OracleDataAdapter(objCmd);
dset = new DataSet();
objAdp.Fill(dset);
objConn.Close();
GridView1.DataSource = dset;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
return;
}
}
}