0

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?

2 Answers 2

0

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;
0
///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());
                }
            }
         }
1
  • I don't see any attempt to answer the question. What's the purpose of your "answer"? You should at least explain your code, just pasting some lines of code is seldom considered helpful.
    – jps
    Jun 21, 2018 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.