0

I want to connect to an Oracle database from a .NET Web API application using OLE DB (if you think of something else that's simple, please feel free).

Connection string:

<add name="OraConnection" connectionString="Provider=MSDAORA.1;User ID=system;password=sa123;Data Source=127.0.0.1;Persist Security Info=False" />

C# code:

try
{
    string cnxString = ConfigurationManager.ConnectionStrings["OraConnection"].ConnectionString;
    string query = "SELECT COUNT(*) FROM TransferedCalls WHERE Agent = '@Agent';";
    using (OleDbConnection conn = new OleDbConnection(cnxString))
    {
        conn.Open();
        using (OleDbCommand comm = new OleDbCommand(q1))
        {
            comm.Connection = conn;
            comm.CommandType = System.Data.CommandType.Text;
            comm.Parameters.AddWithValue("@Agent", "login");
            try
            {
                int x = (Int32)comm.ExecuteScalar();
                if (x > 0)
                    return 1;
            }
            catch (SqlException x)
            {
                return 2;
            }
        }
    }
}
catch (Exception e)
{
    return 2;
}

I'm using a local Oracle XE 11g and I have Oracle instant client 11.0.2 installed.

The exception occurs at this statement conn.Open();:

The client components and Oracle network are not found. These components are supplied by Oracle Corporation in the customer installation of Oracle Version 7.3.3 (or later).

You can not use this provider before installing these components.

Fourat
  • 2,366
  • 4
  • 38
  • 53
  • See http://stackoverflow.com/questions/9002560/oracle-client-and-networking-components-were-not-found – Steve Greene May 23 '16 at 15:09
  • I did installed the oracle client on my machine (32 and 64) but it's the same – Fourat May 23 '16 at 15:29
  • What about the remote server? You might also try a connection string without tnsnames. https://www.connectionstrings.com/oracle-data-provider-for-net-odp-net/using-odpnet-without-tnsnamesora/ – Steve Greene May 23 '16 at 15:49
  • With that kind of connection strings, I get this exception message: ` An OLE DB provider was not specified in ConnectionString. For example, 'Provider = SQLOLEDB;'. ` – Fourat May 23 '16 at 16:50
  • You have the providername attribute? http://stackoverflow.com/questions/16432576/oracle-database-connection-in-web-config-asp-net – Steve Greene May 23 '16 at 17:08
  • Now I got this: Trying to load Oracle client libraries returned BadImageFormatException. This problem occurs in 64-bit mode with 32-bit Oracle client components installed. Also when I try to connect to Oracle server from VS's Server Explorer I get the same message. – Fourat May 23 '16 at 17:17
  • Very common problem - you need to run in 32 bit mode. Personally, I would switch to the managed driver to avoid the issue of matching bit-ness. http://stackoverflow.com/questions/24104210/badimageformatexception-this-will-occur-when-running-in-64-bit-mode-with-the-32 – Steve Greene May 23 '16 at 18:07

0 Answers0