[英]How to connect to Oracle with C# using a LDAP/OID connection string
I have two questions regarding connecting to an Oracle database using C#.NET.关于使用 C#.NET 连接到 Oracle 数据库,我有两个问题。
My program references Oracle.DataAccess.dll (version 2.122.19.1) in my Oracle 19c client 64-bit folder.我的程序在我的 Oracle 19c 客户端 64 位文件夹中引用了 Oracle.DataAccess.dll(版本 2.122.19.1)。
I have a using statement:我有一个使用语句:
using Oracle.DataAccess.Client;使用 Oracle.DataAccess.Client;
and this code to connect:和此代码连接:
string connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.sys.mycompany.com)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=mydb.mycompany.com)));User Id=MyLanId;Password=MyPasswors;Validate Connection=True;"
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
}
Question #1:问题 #1:
How do I create an OID connection string that does not specify the DB name but instead queries the LDAP for the detail server/port info that is hardcoded in my TNS-less connection string above?如何创建一个 OID 连接字符串,它不指定数据库名称,而是查询 LDAP 以获取在上面的无 TNS 连接字符串中硬编码的详细服务器/端口信息?
For example, in a Java program, we connect like this.比如在Java程序中,我们是这样连接的。
base.oracle.db.url=jdbc:oracle:thin:@ldap://oid.gtm.internal.mycompany.com:3060/dbname,cn=OracleContext,dc=mycompany,dc=com base.oracle.db.url=jdbc:oracle:thin:@ldap://oid.gtm.internal.mycompany.com:3060/dbname,cn=OracleContext,dc=mycompany,dc=com
What would the .NET equivalent be? .NET 的等价物是什么?
Question Number #2:问题 #2:
When I compile my C# program I selected "Any CPU" yet my program only works when I reference the 64-bit Oracle.DataAccess.dll.当我编译我的 C# 程序时,我选择了“Any CPU”,但我的程序仅在我引用 64 位 Oracle.DataAccess.dll 时才有效。 Is this because my PATh variable mentions my 64-bit oracle client folder before my 32-bit folder (I have both)?
这是因为我的 PATh 变量在我的 32 位文件夹之前提到了我的 64 位 oracle 客户端文件夹(我都有)? Right now, my PATH variable is way over the max length of 2048 and I cannot change it without making it shorter and I'm not sure what to delete.
现在,我的 PATH 变量超过了 2048 的最大长度,如果不缩短它我就无法更改它,而且我不确定要删除什么。
I found this SO post,我发现了这个 SO 帖子,
How do I query LDAP from C# to resolve Oracle TNS hostname while using managed ODP.NET? 如何在使用托管 ODP.NET 时从 C# 查询 LDAP 以解析 Oracle TNS 主机名?
Instead of just passing in a connection string that points to an LDAP server, I can do an LDAP query to get the db descriptor to build the connection string.除了传递指向 LDAP 服务器的连接字符串外,我还可以执行 LDAP 查询来获取数据库描述符以构建连接字符串。
It's a 2 step process, so I probably want to cache the connection string once I build it.这是一个两步过程,所以我可能想在构建连接字符串后缓存它。 I normally count on connection pooling and "close" connections when I am done with them but this two step process seems like I have to manually add some caching of the connection string to avoid the overhead of hitting LDAP multiple times.
当我完成它们时,我通常指望连接池和“关闭”连接,但是这两个步骤的过程似乎我必须手动添加一些连接字符串的缓存以避免多次点击 LDAP 的开销。
Is there a better way?有没有更好的办法?
string directoryServer = "oid.gtm.internal.mycompany.com:3060";
string defaultAdminContext = "cn=OracleContext,dc=mycompany,dc=com";
string serviceName = "mydb";
string userId = "mylanid";
string password = "mypwd";
using (IDbConnection connection = GetConnection(directoryServer, defaultAdminContext, serviceName, userId, password))
{
connection.Open();
connection.Close();
}
private static IDbConnection GetConnection(string directoryServer, string defaultAdminContext,
string serviceName, string userId, string password)
{
string descriptor = ConnectionDescriptor(directoryServer, defaultAdminContext, serviceName);
// Connect to Oracle
string connectionString = $"user id={userId};password={password};data source={descriptor}";
OracleConnection con = new OracleConnection(connectionString);
return con;
}
private static string ConnectionDescriptor(string directoryServer, string defaultAdminContext,
string serviceName)
{
string ldapAdress = $"LDAP://{directoryServer}/{defaultAdminContext}";
string query = $"(&(objectclass=orclNetService)(cn={serviceName}))";
string orclnetdescstring = "orclnetdescstring";
DirectoryEntry directoryEntry = new DirectoryEntry(ldapAdress, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry, query, new[] { orclnetdescstring },
SearchScope.Subtree);
SearchResult searchResult = directorySearcher.FindOne();
byte[] value = searchResult.Properties[orclnetdescstring][0] as byte[];
if (value != null)
{
string descriptor = Encoding.Default.GetString(value);
return descriptor;
}
throw new Exception("Error querying LDAP");
}
Once you created oracle connection successfully成功创建 oracle 连接后
OracleConnection con = new OracleConnection(connectionString); OracleConnection con = new OracleConnection(connectionString);
How to get data from it with a sql:如何使用sql从中获取数据:
string strSQL="select * from users"; string strSQL="select * from users";
I use the following codes and no data return我使用以下代码没有数据返回
con.Open();
OleDbDataAdapter oDA = new OleDbDataAdapter(strSQL, con);
DataSet ds = new DataSet();
oDA.Fill(ds);
con.Close();
Please advise.请指教。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.