Tuesday, September 10, 2013

How to connect to Oracle using ASP C#

You must have oracle client for .NET developer tools visual studio etc.

You can download oracle client from here (ODAC) Oracle Developer Tools for Visual Studio
for you oracle version.
http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html

when oracle client installed appears reference Oracle.DataAccess.dll  on your visual studio solution explorer. Please add this reference 














You can add connection string on c# code now.
using reference ================================================================



using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Oracle.DataAccess.Client;

connection string declare public variable on aspx.cs file, if you want add this strings to web.config file  ================================================


string strOracle_con = "Data Source=(DESCRIPTION="
             + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.10)(PORT=1521)))"
             + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));"
             + "User Id=user_name;Password=pass;";

now connection string is OK, 
let's get some data from database ===========================================

protected void Button1_Click(object sender, EventArgs e)
{

string sql = "select * from user_schema.t_table";
OracleConnection conn = new OracleConnection(strOracle_con);
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
conn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
conn.Close();
conn.Dispose();

if you have occurred error as below
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

fix this error as below command from start-run-cmd
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i


 That's it

 


No comments:

Post a Comment