通过C#连接到Oracle数据库? [英] Connecting to Oracle Database through C#?
问题描述
我需要通过Visual Studio 2010中连接到Oracle数据库(外部),但我不希望我的机器上安装Oracle。
在项目中,我引用: System.Data.OracleClient的。但其不履行的需要。
我有一个的Oracle SQL Developer IDE我在其中运行针对Oracle数据库的SQL查询。
I need to connect to a Oracle DB (external) through Visual Studio 2010. But I dont want to install Oracle on my machine. In my project I referenced: System.Data.OracleClient. But its not fulfilling the need. I have an "Oracle SQL Developer IDE" in which I run SQL queries against oracle db.
我有这样的code迄今:
I have this code so far:
private static string GetConnectionString()
{
String connString = "host= serverName;database=myDatabase;uid=userName;pwd=passWord";
return connString;
}
private static void ConnectingToOracle()
{
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 myTableName";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["MYFIELD"];
Console.WriteLine(myField);
}
}
}
到目前为止,我阅读这些博客:
So far I read these blogs:
<一个href=\"http://st-curriculum.oracle.com/tutorial/DBXETutorial/index.htm\">http://st-curriculum.oracle.com/tutorial/DBXETutorial/index.htm
<一个href=\"http://blogs.msdn.com/b/kaevans/archive/2009/07/18/connecting-to-oracle-from-visual-studio.aspx\">http://blogs.msdn.com/b/kaevans/archive/2009/07/18/connecting-to-oracle-from-visual-studio.aspx
到目前为止,我还没有下载从Oracle什么。我应该采取什么步骤来做到这一点?
So far I have not downloaded anything from Oracle. What steps should I take to make this happen?
推荐答案
首先,你需要下载并从此站点安装ODP
http://www.oracle.com/technetwork/topics/dotnet/index -085163.html
First off you need to download and install ODP from this site http://www.oracle.com/technetwork/topics/dotnet/index-085163.html
在安装后添加组件的参考 Oracle.DataAccess.dll
After installation add a reference of the assembly Oracle.DataAccess.dll.
您有良好的售后服务这个去的。
Your are good to go after this.
using System;
using Oracle.DataAccess.Client;
class OraTest
{
OracleConnection con;
void Connect()
{
con = new OracleConnection();
con.ConnectionString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
con.Open();
Console.WriteLine("Connected to Oracle" + con.ServerVersion);
}
void Close()
{
con.Close();
con.Dispose();
}
static void Main()
{
OraTest ot= new OraTest();
ot.Connect();
ot.Close();
}
}
这篇关于通过C#连接到Oracle数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!