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

 


How to connect to mysql using asp c#

You need things as below
- mysql database access user
- mysql connector dll file: MySql.Data.dll
- visual studio 9.0 2008

1. You can add MySql.Data.dll reference from solution explorer, if there is not you can download here
 if download is not working notify me pls



2. add connection string on web.config file


<connectionStrings>
    <add name="constr_mysql" connectionString="server=192.168.0.10;uid=root; pwd=pass;database=db_name; Allow Zero Datetime=true" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

3. Using connection string.

add using reference on c# code  ========================================



using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

Declare mysql connection string on c# code =============================


string strConnectionString = ConfigurationManager.ConnectionStrings["constr_mysql"].ConnectionString;



click event on some button  ============================================


MySqlConnection conn = new MySqlConnection(strConnectionString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "select * from tbl_name;";
conn.Open();
Repeater1.DataSource = command.ExecuteReader();
Repeater1.DataBind();
conn.Close();


That's it :)