简体   繁体   中英

How to connect to Oracle database

I am using below code to connect to an oracle server. I need to particularly use OdbcConnection class because I am doing enhancement of an existing application which currently connects to SQL Server using this method. So I have to use the same method for other DBMS also.

I have tried with the code below:

const string ConnectionString = @"SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MachineName.Domain.com)(PORT=1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME=orcl.Domain.com)));uid=system;pwd=user";

using (OdbcConnection connection = new OdbcConnection(ConnectionString))
    {
    connection.Open();
    }

But I get an exception when calling the Open() method as follows:

"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"

Any idea of what I am doing wrong here?

Have a look at Oracle connection strings

You connectionString should be like this:

var DB = @"SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MachineName.Domain.com)(PORT=1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME=orcl.Domain.com)))";
string ConnectionString = "Driver={Oracle in OraClient11g_home1};Dbq=" + DB + ";uid=system;pwd=user;"

The driver name might be different on your machine. You could also use the ODBC driver from Microsoft ( Driver={Microsoft ODBC for Oracle};Server=... ), however this is deprecated for ages.

Actually I prefer the DbConnectionStringBuilder (or even OdbcConnectionStringBuilder )

var str = new DbConnectionStringBuilder(true);
str.Add("Driver", "Oracle in OraClient11g_home1");
str.Add("Dbq", @"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MachineName.Domain.com)(PORT=1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME=orcl.Domain.com)))");
str.Add("Uid", "system");
str.Add("Pwd", "user");
string ConnectionString = str.ConnectionString;
///Your connection string should be like           
string str = Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = abc )(PORT = 123))(CONNECT_DATA =(SID = xyz)));User Id=abc_xyz;Password=111;Min Pool Size=10;Connection Lifetime=120;Connection Timeout=600;Incr Pool Size=5; Decr Pool Size=2;validate connection=true;

   using (OracleConnection con = new OracleConnection(str))
        {
            using (OracleCommand cmd = con.CreateCommand())
            {
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = RoutineConstant.Text;
                cmd.BindByName = true;

                cmd.ExecuteNonQuery();
                if (cmd.Parameters["Value"].Value != null && cmd.Parameters["Value"].Value != DBNull.Value)
                {
                    return Convert.ToDecimal(cmd.Parameters["Value"].Value.ToString());
                }
            }
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM