0
votes

I have a database called "sistema" using application apex express, I am trying to connect with this code:

private void button1_Click(object sender, EventArgs e)
{
    string constr = "Data Source=sistema;User Id=admin;Password=123;";
    string ProviderName = "Oracle.ManagedDataAccess.Client";

    using (OracleConnection conn = new OracleConnection(constr))
    {
        try
        {
            conn.ConnectionString = constr;
            conn.Open();

            //Get all the schema collections and write to an XML file. 
            //The XML file name is Oracle.ManagedDataAccess.Client_Schema.xml
            DataTable dtSchema = conn.GetSchema();
            dtSchema.WriteXml(ProviderName + "_Schema.xml");

            MessageBox.Show("YEAH");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            MessageBox.Show(ex.StackTrace);
        }
    } 
}

This code shows me this error:

ORA-12154: TNS could not resolve the specified connection identified

With this new code:

    private void button1_Click(object sender, EventArgs e)
    {
        string constr = @"Data Source=(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost )(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=SISTEMA)));
User Id=ADMIN ;Password=123";
        string ProviderName = "Oracle.ManagedDataAccess.Client";

        using (OracleConnection conn = new OracleConnection(constr))
        {
            try
            {
                conn.ConnectionString = constr;
                conn.Open();

                //Get all the schema collections and write to an XML file. 
                //The XML file name is Oracle.ManagedDataAccess.Client_Schema.xml
                DataTable dtSchema = conn.GetSchema();
                dtSchema.WriteXml(ProviderName + "_Schema.xml");

                MessageBox.Show("YEAH");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
        } 
    }

It returns me

User id 'is an invalid connection string attribute

When I try to connect to the database from VS I get this error:

The listener does not currently know the requested service

The database works correctly on Oracle Apex http://localhost:8080/apex and has tables and records created Also try with uppercase and lowercase names and nothing changes

I do not understand how to connect to my Oracle Apex database in application express, it is confusing, I do not know what to change with respect to normal databases in oracle.

How can I connect to my apex database in c# ?

2
I am not sure but try User ID instead of User Id and remove any space character from connection string.Wernfried Domscheit

2 Answers

0
votes

Problem is your not putting the tns identifier at the right place. If you had added the oracle manage data access package from nuget there will be an added configuration line on your app config/web config.

<oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" 
                    descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))
                    (CONNECT_DATA=(SERVICE_NAME=ORCL))) " />
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>

Edi the alias to "sistema" and put your tns connection identifier on the descriptor. like this

<dataSource alias="sistema" 
            descriptor="(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost )(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=SISTEMA))) " />
          </dataSources>

and use your fist attempt connection string. That will resolve TNS identification error.

0
votes

OracleConnection cannot resolve the alias. According documentation there are several possibilities to resolve the name in following order:

  1. data source alias in the dataSources section under <oracle.manageddataaccess.client> section in the .NET config file (i.e. machine.config, web.config, user.config).

    -> this was already provided by yonas in previous answer

  2. data source alias in the tnsnames.ora file at the location specified by TNS_ADMIN in the .NET config file. Locations can consist of either absolute or relative directory paths.
  3. data source alias in the tnsnames.ora file present in the same directory as the .exe.

Actually the documentation is not fully correct, or let's say "not thorough". Item 2 and 3 applies only if you use local tnsnames.ora file. However, in file sqlnet.ora you may specify different NAMES.DIRECTORY_PATH, e.g. LDAP or EZCONNECT.

So, actually it should be like this

  1. data source alias in the tnsnames.ora file resp. naming method as defined in sqlnet.ora file at the location specified by TNS_ADMIN in the .NET config file. Locations can consist of either absolute or relative directory paths.
  2. data source alias in the tnsnames.ora file resp. naming method as defined in sqlnet.ora file present in the same directory as the .exe.