Reputation: 13
protected void Page_Load(object sender, EventArgs e)
{
OracleConnection con = new OracleConnection();
con.ConnectionString = "User id =test;password=test1;Datasource=oracle";
myConnection.Open();
}
Above is the code that I am using. It will be called on page_Load.
Upvotes: 0
Views: 160
Reputation: 4215
Follow below steps,below is my sample example:
1.In Web.config of your file add below string
<connectionStrings>
<add name="CustomerDataConnectionString" connectionString="Data Source=.;User Id=*;Password=*;Integrated Security=SSPI;Initial Catalog=Northwind;OLEDB.NET=True" providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
//* must be filled with your credentials
2.Now in the code behind file,Import namespace for oracle client and Configuration manager for oracle client and below code
using System.Data.OracleClient;
using System.Data;
using System.Configuration;
3.Write below code in your Page_Load event:Cmd can be SQL command
static string strConnectionString = ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString;
using (OracleConnection con = new OracleConnection(strConnectionString))
{
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
{
table = new DataTable();
da.Fill(table);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
Refer this link http://www.connectionstrings.com/ for more inforamtion
Upvotes: 1
Reputation: 2715
string oradb = "User id =test;password=test1;Datasource=oracle";
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
using (OracleDataAdapter a = new OracleDataAdapter(
"SELECT id FROM emp1", conn))
{
DataTable t = new DataTable();
a.Fill(t);
// Render data onto the screen
dataGridView1.DataSource = t;
}
conn.Dispose();
Upvotes: 0
Reputation: 618
Make sure you have included the required libraries,
try using this code ,
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test;";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
It's always better to have try-catch
. that helps you track the exact error if you are stuck somewhere.
Upvotes: 0