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?
source to share
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
source to share