开发者

Connecting to Oracle from Visual Studio 2010

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜