How to include sid and port in oracle connection string?

I would like to specify the port and sid in the connection string. After executing the following code

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 () is not responding ... The problem is in the side, I think. What could be the problem?

UPDATE: I have to use this connection string. But I cannot interpret it.

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

Can anyone help me interpret this?

+3


source to share


4 answers


I had to replace SERVICE_NAME with SID, so this:

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);

      



did the trick.

+3


source


Let's take what you have here.

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

      

This is the connection string .net

This part is here

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

      

is that the oracle client needs to connect to the SID. This part can also be customized in the file TNS Names

. In this case, you would have something like

MyOraDbConnection = (DESCRIPTION=(ADDRESS_LIST=...

      

So your code .net

will look like

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

      



2

Now it looks like you want to do things dynamically. Usually people will take a bunch of textbox values ​​and concatenate them like this

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

      

another way:

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);

      

Or, you can create a ConnStr object that can do more than just concatenate strings. It can store your conn string - in pseudocode

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. 
    }
}

      

It's more work, but also more flexible

+3


source


To avoid the TNS format, try this:

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

      

0


source


Try this connection string:

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

      

-1


source







All Articles