我想使用ODBC从Visual Studio 2010连接到Oracle 11g数据库。我无法连接ODP.NET,所以我想尝试使用ODBC。有人可以告诉我这涉及到哪些步骤?
答案 0 :(得分:3)
假设您使用的是C#,
您必须在项目中添加对 System.Data.OracleClient.dll 的引用
以下是一些示例样板代码
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);
}
}
}
来源 - http://www.codeproject.com/KB/database/C__Instant_Oracle.aspx