0

I'm beginner about web programming and I have a problem with oracle db connection. My code is working well with Mssql but it's not working with oracle. I want to change db to oracle from mssql. I have downloaded odac. Here is my SqlOperations class.

using System;
using System.Collections.Generic;
using System.Data;
using Oracle.DataAccess;
using System.Data.OracleClient;
using System.Data.Odbc;
using System.Linq;
using System.Web;

namespace BagciEmlak
{
public class SqlOperations
{
    OracleConnection con;
    OracleCommand cmd;
    OracleDataAdapter sda;
    DataTable dt;
    public OracleConnection OracleConnect()
    {
        try
        {
            con = new OracleConnection ("Data Source=SYS-CDB12c; User ID=HR; Password=hr; Unicode=true;");
            con.Open();
            return con;
        }
        catch (OracleException e)
        {

            throw;
        }

    }
    public int Command(string ConStr, OracleConnection conn)
    {
        try
        {
            cmd = new OracleCommand(ConStr, conn);

            return cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {

            throw;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }


    }
    public DataTable GetDataTable(string ConStr, OracleConnection conn)
    {
        try
        {
            dt = new DataTable();
            sda = new OracleDataAdapter(ConStr, conn);
            sda.Fill(dt);

            return dt;
        }
        catch (Exception e)
        {

            throw;
        }
        finally
        {
            sda.Dispose();
            conn.Close();
            conn.Dispose();
        }

    }
    public DataRow GetDataRow(string ConStr, OracleConnection conn)
    {
        dt = GetDataTable(ConStr, conn);
        if (dt.Rows.Count==0)
        {
            return null;
        }
        else
        {
            return dt.Rows[0];
        }
    }
    public string GetDataCell(string ConStr, OracleConnection conn)
    {
        dt = GetDataTable(ConStr, conn);
        if (dt.Rows.Count == 0)
        {
            return null;
        }
        else
        {
            return dt.Rows[0][0].ToString();
        }
    }

}

}

and here is my webconfig code:

<connectionStrings>
    <add name="{ConnectionName}" 
    connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=HR;Password=HR;" 
    providerName="Oracle.DataAccess.Client"/>
 </connectionStrings>

So whats wrong within can you help me

3
  • When i compile there is not an error but in chrome i saw that : Server Error in '/' Application. Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format. Commented May 18, 2017 at 19:27
  • System.Data.OracleClient is deprecated. Don't use it. Use the Oracle Managed Client. And do not store your IDiposable (OracleConnection) objects as fields. And of course, why are you using DataTable instead of strongly typed objects? Commented May 18, 2017 at 19:32
  • to you have a TNSNAMES file also download the Oracle.Data.Client for 32bit.. and you can use the connection string the same way one would with Sql Server etc.. I use Oracle and Sql Server on a daily basis.for example depending on what your project settings are you could change it to x86, then change your config file to look different.. for example <connectionStrings> <add name="DbConn" connectionString="Data Source=DBANAME;User Id=userId;Password=yourPassword;"/> Commented May 18, 2017 at 19:38

1 Answer 1

1

Just change parameter in OracleConnection to

SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SYS-CDB12c)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID))); uid=HR;pwd=hr;

Also change myOracleSID to your oracle database instance name.

In your case connection string in web.config are not concerns to your connection. Because you pass connection as string parameter whitout using configuration manager.

Sign up to request clarification or add additional context in comments.

5 Comments

I did all but this error not solved. Then i found its about 32 bit/64bit problem. When i change platform target x86 format probably this error is solving. But now i am taking this exeption :System.NullReferenceException: 'Object reference not set to an instance of an object. Do you have any idea about it? I checked my username and pw but its true...
And show please your web config connectionstring section.
As i understand now problem in configuration manager. It cant find connection string by passed name.
No. I ask for web.config content ). Check name of connection string in your code and in web.config. are they are same?
Change connection string name in webConfig to connectionString. Without {} scopes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.