写了一个c#程序,需要在其中建立和特定数据库的连接
因为程序需要在不同的电脑中使用,所以想仅仅依赖IP地址和端口以及实例名来进行连接
或者说不想依赖于sqlplus和本地建立的数据库服务名来建立连接
请问有没有什么比较好的办法现在我的语句是
string constring = "data source=10.0.192.23:1521/km; user=testuser1; password=aaa111";
可是在其他的电脑上测试结果是无法解析

解决方案 »

  1.   

    在Config中设置 <connectionStrings>
        <add name="Default" connectionString="Data Source=km;User user=testuser1; password=aaa111;Unicode=True"/>
      </connectionStrings>
      

  2.   


    PROVIDER=MSDAORA.1;Password=aaa111;User   ID=testuser1;Data     Source=10.0.192.23:1521/km;Persist   Security   Info=True
      

  3.   

    Oracle不是微软的产品,你既然要用它就得迁就它,JAVA没问题,因为有jre包可以直接调用,不需要安装任何客户端连接程序,但是.NET没办法调用jre包啊,要用到COM组件注册才行,怎么能不安装客户端连接程序呢?其实网上有精简版的客户端安装程序,你可以考虑装那个:
    http://119.147.41.16/down?cid=9F1E669780C3EA212D9F96A4D2626509B37B07E2&t=2&fmt=-
      

  4.   

    倒是没有说不安装Oracle客户端
    只是想说,因为一个数据库在每台电脑上面配置的服务名可能都不同,所以不像依赖于服务名
      

  5.   

    那就(local)就行了,这就是本机
      

  6.   

    指定数据源属性This section describes different ways of specifying the data source attribute.The following example shows a connect descriptor mapped to a TNS alias called sales in the tnsnames.ora file:sales=
     (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.
      

  7.   

    通常这种有客户端的程序,还需要安装大量客户端数量的程序,不建议直接连数据库,这么做,db的连接数会很大,比如1000个客户端,就会对数据库至少造成1000个链接,建议使用一个Server端来处理数据库执行,用socket在client和客户端之间通信。这样可以提高效率,也能不让客户机器都安装Oracle客户端。
      

  8.   


    我倒觉得依赖于服务名更好,毕竟配置数据库连接不可能让用户自己来,你肯定要帮他配,服务名取得好的话,很容易看出是什么数据库的。另外那个服务名是在tnsnames.ora中设置的,你可以直接用C#打开那个文件,判断里面是否存在你需要的服务名,不存在就给他加一个,这不难做到吧。至于Oracle客户端安装路径,读取注册表就可以知道了。
      

  9.   

    Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
      

  10.   

    用这个就可以不用配置 ora文件
      

  11.   

    假设你要查找的Oracle服务名和数据库名都叫km,实现代码如下:
                  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);