如何将sid和port包含在oracle连接字符串中?

时间:2014-12-31 15:42:21

标签: .net oracle connection-string

我想在连接字符串中指定端口和sid。以下代码运行后

public static string ConnectionString
{
    get
    {
        string host = Config.CsHost;
        string sid = Config.CsSID;
        string port = Config.CsPort;
        string user = Config.CsUser;
        string pass = Config.CsPassword;
        return String.Format(@"Data Source = {0}:{1}\{2}; Persist Security Info = True; User Id = {3}; Password = {4}; Unicode = True", host, port, sid, user, pass);
    }
}

...

using (OracleConnection connection = new OracleConnection(ConnectionString))
{
    try
    {
        connection.Open();

Open()没有回应......问题在于我认为sid。可能是什么问题?

更新: 我应该使用这种连接字符串。但我无法解释它。

数据源=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = MyHost)(PORT = MyPort)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = MyOracleSID))); 用户ID = myUsername;密码= myPassword;

有人可以帮我解释一下吗?

4 个答案:

答案 0 :(得分:4)

让我们拿走你在这里的东西

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

这是.net连接字符串 这部分在这里

(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID))) 

是oracle客户端连接到SID所需的内容。此部分也可以在TNS Names文件中配置。在这种情况下,您将拥有类似

的内容
MyOraDbConnection = (DESCRIPTION=(ADDRESS_LIST=...

因此,您的.net代码将如下所示

string connStr = "Data Source=MyOraDbConnection;User Id=myUsername;Password=myPassword;"

<强> 2

现在看起来你想要动态地做事情。通常,人们会使用一堆文本框值并将它们连接为

string dataSource = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + txtDbServer.Text + ...

另一种方式是

string dataSource = string.Format("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={2})))",
    txtDbServer.Text,
    txtPort.Text,
    txtSid.Text);

或者,您可以创建ConnStr对象,它不仅可以连接字符串。它可以保存你的conn字符串 - 伪代码

class ConnStr
{
    string Server {get;set;}
    string Port {get;set;}
    string Sid {get;set;}
    // more properties

    string GetConnectionString()
    {
        // return your compiled string
    }

    void Save(string switches)
    {
        // Save your string to different places. 
        // For example 
        //      /f myconnfile.txt - will save to application root directory
        //      /f c:\xxx\myconnfile.txt - will save to specific directory
        //      /s myconnsetting  - will save to settings
    }

    void Load(string switches)
    {
        // Load your string from sources. 
    }
}

这是更多的工作,但也更灵活

答案 1 :(得分:4)

我必须将SERVICE_NAME替换为SID,所以:

return String.Format("SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))(CONNECT_DATA=(SID={2})));uid={3};pwd={4};", host, port, sid, user, pass);

做了这个伎俩。

答案 2 :(得分:4)

要避免使用TNS格式,请尝试以下操作:

Data Source = server:port/SID; User ID = user; Password = mypwd

答案 3 :(得分:0)

尝试此连接字符串:

User Id=[user];Password=[pass];Server=[server];Direct=True;Sid=[sid];Port=[port];Persist Security Info=True