通过 C# 连接到 Oracle 数据库? [英] Connecting to Oracle Database through C#?
问题描述
我需要通过 Visual Studio 2010 连接到 Oracle DB(外部).但我不想在我的机器上安装 Oracle.在我的项目中,我引用了:System.Data.OracleClient.但它不能满足需要.我有一个 Oracle SQL Developer IDE",我在其中针对 oracle db 运行 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.
到目前为止我有这个代码:
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:
http://st-curriculum.oracle.com/tutorial/DBXETutorial/index.htm一个>
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?
推荐答案
首先你需要从这个站点下载并安装 ODPhttp://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.
你很高兴去做这件事.
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屋!