如何将 sid 和 port 包含到 oracle 连接字符串中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27723223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include sid and port into an oracle connection string?
提问by Mitulát báti
I would like to specify the port and sid in the connection string. After the following code runs
我想在连接字符串中指定端口和 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();
the Open() doesn't respond... The problem is with the sid I think. What might be the problem?
Open() 没有响应......我认为问题出在 sid。可能是什么问题?
UPDATE: I should use this kind of connection string. But I can't interpret it well.
更新:我应该使用这种连接字符串。但我不能很好地解释它。
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID))); User Id=myUsername;Password=myPassword;
数据源=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID))); 用户 ID=我的用户名;密码=我的密码;
Can someone help me interpret this?
有人可以帮我解释一下吗?
采纳答案by Mitulát báti
I had to replace SERVICE_NAME to SID, so this:
我不得不将 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);
did the trick.
成功了。
回答by T.S.
Lets 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 .net
connection string
This part here
这是.net
连接字符串 这部分在这里
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)))
is what oracle client needs to connect to SID. This part can be also configured in TNS Names
file. In this case you will have something like
是 oracle 客户端需要连接到 SID 的内容。这部分也可以在TNS Names
文件中配置。在这种情况下,您将有类似的东西
MyOraDbConnection = (DESCRIPTION=(ADDRESS_LIST=...
So your .net
code will look like
所以你的.net
代码看起来像
string connStr = "Data Source=MyOraDbConnection;User Id=myUsername;Password=myPassword;"
2
2
Now, it looks looks like you want to do stuff dynamically. Usually, people take bunch of text box values and concatenate them as this
现在,看起来您想要动态地做事。通常,人们采用一堆文本框值并将它们连接成这样
string dataSource = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + txtDbServer.Text + ...
another way is
另一种方式是
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 ConnStr object which can do more than just concatenate strings. It may save your conn string - in pseudo-code
或者,您可以创建 ConnStr 对象,它可以做的不仅仅是连接字符串。它可能会保存您的连接字符串 - 在伪代码中
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.
}
}
This is more work but also more flexibility too
这是更多的工作,但也更灵活
回答by Mike Twc
To avoid TNS format try this:
为了避免 TNS 格式,试试这个:
Data Source = server:port/SID; User ID = user; Password = mypwd
回答by Silvinus
Try this connection string :
试试这个连接字符串:
User Id=[user];Password=[pass];Server=[server];Direct=True;Sid=[sid];Port=[port];Persist Security Info=True