从 Visual Studio 2010 连接到 Oracle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7639123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to Oracle from Visual Studio 2010
提问by user965767
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?
我想使用 ODBC 从 Visual Studio 2010 连接到 Oracle 11g 数据库。我无法使用 ODP.NET 进行连接,因此我想尝试使用 ODBC。有人可以告诉我这涉及哪些步骤吗?
回答by dhaivat
Assuming you are using C#,
假设您使用的是 C#,
You will have to add a reference to System.Data.OracleClient.dllin your project
您必须在项目中添加对System.Data.OracleClient.dll的引用
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
来源 - http://www.codeproject.com/KB/database/C__Instant_Oracle.aspx