简体   繁体   English

使用C#连接到Oracle APEX数据库

[英]Connect to Oracle APEX database in C#

I have a database called "sistema" using application apex express, I am trying to connect with this code: 我有一个使用应用程序顶点快递的名为“ sistema”的数据库,我正在尝试与此代码连接:

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: 当我尝试从VS连接到数据库时,出现此错误:

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 该数据库可在Oracle Apex http:// localhost:8080 / apex上正常工作,并已创建表和记录。请尝试使用大写和小写名称,并且没有任何变化

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. 我不了解如何在应用程序快递中连接到我的Oracle Apex数据库,这令人困惑,我不知道相对于oracle中的普通数据库要更改什么。

How can I connect to my apex database in c# ? 如何在c#中连接到我的apex数据库?

Problem is your not putting the tns identifier at the right place. 问题是您没有将tns标识符放在正确的位置。 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. 如果您已经从nuget添加了oracle manage data access package,那么您的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. 将别名编辑为“ sistema”,然后将tns连接标识符放在描述符上。 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. 这样可以解决TNS识别错误。

OracleConnection cannot resolve the alias. OracleConnection无法解析别名。 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 (ie machine.config , web.config , user.config ). .NET配置文件(即machine.configweb.configuser.config )的<oracle.manageddataaccess.client>部分下的dataSources部分中的数据源别名。

    -> this was already provided by yonas in previous answer -> yonas在先前的回答中已经提供了

  2. data source alias in the tnsnames.ora file at the location specified by TNS_ADMIN in the .NET config file. tnsnames.ora文件中.NET配置文件中TNS_ADMIN指定的位置的数据源别名。 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 . tnsnames.ora文件中的数据源别名与.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. 仅当您使用本地tnsnames.ora文件时,项目2和3才适用。 However, in file sqlnet.ora you may specify different NAMES.DIRECTORY_PATH , eg LDAP or EZCONNECT . 但是,可以在文件sqlnet.ora指定其他NAMES.DIRECTORY_PATH ,例如LDAPEZCONNECT

So, actually it should be like this 所以,实际上应该是这样

  1. data source alias in the tnsnames.ora file resp. tnsnames.ora文件中的数据源别名 naming method as defined in sqlnet.ora file at the location specified by TNS_ADMIN in the .NET config file. 在.NET配置文件中TNS_ADMIN指定的位置处, sqlnet.ora文件定义的命名方法 Locations can consist of either absolute or relative directory paths. 位置可以包含绝对或相对目录路径。
  2. data source alias in the tnsnames.ora file resp. tnsnames.ora文件中的数据源别名 naming method as defined in sqlnet.ora file present in the same directory as the .exe . .exe在同一目录中的sqlnet.ora文件中定义的命名方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM