写了一个c#程序,需要在其中建立和特定数据库的连接
因为程序需要在不同的电脑中使用,所以想仅仅依赖IP地址和端口以及实例名来进行连接
或者说不想依赖于sqlplus和本地建立的数据库服务名来建立连接
请问有没有什么比较好的办法现在我的语句是
string constring = "data source=10.0.192.23:1521/km; user=testuser1; password=aaa111";
可是在其他的电脑上测试结果是无法解析
因为程序需要在不同的电脑中使用,所以想仅仅依赖IP地址和端口以及实例名来进行连接
或者说不想依赖于sqlplus和本地建立的数据库服务名来建立连接
请问有没有什么比较好的办法现在我的语句是
string constring = "data source=10.0.192.23:1521/km; user=testuser1; password=aaa111";
可是在其他的电脑上测试结果是无法解析
<add name="Default" connectionString="Data Source=km;User user=testuser1; password=aaa111;Unicode=True"/>
</connectionStrings>
PROVIDER=MSDAORA.1;Password=aaa111;User ID=testuser1;Data Source=10.0.192.23:1521/km;Persist Security Info=True
http://119.147.41.16/down?cid=9F1E669780C3EA212D9F96A4D2626509B37B07E2&t=2&fmt=-
只是想说,因为一个数据库在每台电脑上面配置的服务名可能都不同,所以不像依赖于服务名
(DESCRIPTION=
(ADDRESS= (PROTOCOL=tcp)(HOST=sales-server)(PORT=1521))
(CONNECT_DATA=
(SERVICE_NAME=sales.us.acme.com)))
使用 TNS 别名To connect as scott/tiger using the TNS Alias, a valid connection appears as follows:"user id=scott;password=tiger;data source=sales";
使用连接描述符ODP.NET also allows applications to connect without the use of the tnsnames.ora file. To do so, the entire connect descriptor can be used as the "data source".The connection string appears as follows:"user id=scott;password=tiger;data source=" +
"(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)" +
"(HOST=sales-server)(PORT=1521))(CONNECT_DATA="+
"(SERVICE_NAME=sales.us.acme.com)))"
使用便捷命名方式The easy connect naming method enables clients to connect to a database without any configuration.Prior to using the easy connect naming method, make sure that EZCONNECT is specified by the NAMES.DIRECTORY_PATH parameter in the sqlnet.ora file as follows:NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
With this enabled, ODP.NET allows applications to specify the "Data Source" attribute in the form of://host:[port]/[service_name]Using the same example, some valid connection strings follow:"user id=scott;password=tiger;data source=//sales-server:1521/sales.us.acme.com"
"user id=scott;password=tiger;data source=//sales-server/sales.us.acme.com"
"user id=scott;password=tiger;data source=sales-server/sales.us.acme.com"
If the port number is not specified, 1521 is used by default.
我倒觉得依赖于服务名更好,毕竟配置数据库连接不可能让用户自己来,你肯定要帮他配,服务名取得好的话,很容易看出是什么数据库的。另外那个服务名是在tnsnames.ora中设置的,你可以直接用C#打开那个文件,判断里面是否存在你需要的服务名,不存在就给他加一个,这不难做到吧。至于Oracle客户端安装路径,读取注册表就可以知道了。
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftware = hklm.OpenSubKey("Software");
RegistryKey hkOracle = hkSoftware.OpenSubKey("Oracle");
if (hkOracle == null)
{
MessageBox.Show("未找到Oracle客户端安装路径信息。");
return;
}
string OraClient = null;
foreach (string tmp in hkOracle.GetSubKeyNames())
{
if (tmp.Contains("KEY_OraClient"))
{
OraClient = tmp;
break;
}
}
if (OraClient == null)
{
MessageBox.Show("未找到Oracle客户端安装路径信息。");
return;
} String path = (string)hkOracle.OpenSubKey(OraClient).GetValue("ORACLE_HOME");
string tns = File.ReadAllText(Path.Combine(path, @"NETWORK\ADMIN\tnsnames.ora"));
if (!Regex.IsMatch(tns, @"(?s)km\s*=\s*\(.+\)"))
tns += @"
km =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.192.23)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = km)
)
)
";
File.WriteAllText(Path.Combine(path, @"NETWORK\ADMIN\tnsnames.ora"), tns);