简体   繁体   中英

can any one help me how to connect database in oracle and the connection string in c#

  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.

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

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.

        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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM