简体   繁体   中英

Oracle Database Connection

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

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.

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