简体   繁体   English

如何将c#与Oracle数据库连接以指定数据库名称?

[英]How to connect c# with Oracle database specifing the database name?

I have a problem trying to connect to oracle database specifing the database name, when the database name is different than the user name. 当数据库名称不同于用户名时,尝试连接到指定数据库名称的oracle数据库时出现问题。

I used to connect using the next String. 我以前使用下一个String进行连接。 But in this case the Schema of the database was the same as the user name. 但是在这种情况下,数据库的架构与用户名相同。

     String con = "data source= (DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = " + host + ")(PORT = " + port + ")))(CONNECT_DATA =(SERVICE_NAME = " + servicename + ")));
USER ID=" + user + ";
PASSWORD=" + pass;

So the query for login was like 所以登录查询就像

SELECT * FROM usuarios WHERE usuario='545478';

Now i have a new user for the conexion, diferent that the Schema, so now i need to change all the sql queries using the Schema name. 现在,我有一个新用户用于conexion,与该模式不同,所以现在我需要使用模式名称更改所有sql查询。 For example if the schema name is PRODUCTION_DB the query needs to be change to: 例如,如果架构名称为PRODUCTION_DB,则需要将查询更改为:

SELECT * FROM PRODUCTION_DB.usuarios WHERE usuario='545478';

But i dont want to change all the queries in my code. 但是我不想更改代码中的所有查询。 I prefer to specified the database name in the conection like in MySQL. 我更喜欢像在MySQL中那样在锥体中指定数据库名称。

How can i change de String connection to add the database name? 如何更改de String连接以添加数据库名称?

For example: 例如:

String con = "data source= (DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = " + host + ")(PORT = " + port + ")))(CONNECT_DATA =(SERVICE_NAME = " + servicename + ")));
USER ID=" + user + ";PASSWORD=" + pass;
 DATABASE = "PRODUCTION_DB";

Try this connection string: 尝试以下连接字符串:

 string connectionString = "user id=uat;password=*****;data source=localhost:1521/orcl";
 OracleConnection connection = new OracleConnection(connectionString);
 connection.Open();

where: 哪里:

  • orcl - is my database name (SID) orcl-是我的数据库名称(SID)
  • uat - is a schema name (user name) uat-是架构名称(用户名)
  • localhost:1521 - are a server name (in my case: localhost) and a port. localhost:1521-是服务器名称(在我的情况下是:localhost)和端口。

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

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