Connecting to Oracle from Visual Studio 2010

12,636

Assuming you are using C#,

You will have to add a reference to System.Data.OracleClient.dll in your project

Here is some sample boilerplate code,

using System.Data.OracleClient;

static private string GetConnectionString() 
{ 
   // To avoid storing the connection string in your code, 
   // you can retrieve it from a configuration file. 
   return "Data Source=myserver.server.com;Persist Security Info=True;" + 
      "User ID=myUserID;Password=myPassword;Unicode=True"; 
}

// This will open the connection and query the database
static private void ConnectAndQuery() 
{ 
   string connectionString = GetConnectionString(); 
   using (OracleConnection connection = new OracleConnection()) 
   { 
       connection.ConnectionString = connectionString; 
       connection.Open(); 
       Console.WriteLine("State: {0}", connection.State); 
       Console.WriteLine("ConnectionString: {0}", 
                  connection.ConnectionString); 

       OracleCommand command = connection.CreateCommand(); 
       string sql = "SELECT * FROM MYTABLE"; 
        command.CommandText = sql; 

       OracleDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
            string myField = (string)reader["MYFIELD"]; 
            Console.WriteLine(myField); 
       }
   }
}

Source - http://www.codeproject.com/KB/database/C__Instant_Oracle.aspx

Share:
12,636
user965767
Author by

user965767

Updated on June 04, 2022

Comments

  • user965767
    user965767 almost 2 years

    I would like to connect to an Oracle 11g databse from Visual Studio 2010 using ODBC. I was not able to connec tusing ODP.NET, so I want to try using ODBC. Can someone please tell me what are the steps involved in this?