使用 VB.net 连接到 Oracle 10g DB 的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4806322/
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
Connection string to Oracle 10g DB using VB.net
提问by StealthRT
Hey all i am VERY new to a Oracle DB and i am trying to connect to it via VB.net 2010. I have been trying the following:
大家好,我是 Oracle DB 的新手,我正在尝试通过 VB.net 2010 连接到它。我一直在尝试以下操作:
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim dr As OleDbDataReader
myConnection = New OleDbConnection("Provider=MSDAORA.1;UserID=xxxx;password=xxxx; database=xxxx")
'MSDORA is the provider when working with Oracle
Try
myConnection.Open()
'opening the connection
myCommand = New OleDbCommand("Select * from emp", myConnection)
'executing the command and assigning it to connection
dr = myCommand.ExecuteReader()
While dr.Read()
'reading from the datareader
MessageBox.Show("EmpNo" & dr(0))
MessageBox.Show("EName" & dr(1))
MessageBox.Show("Job" & dr(2))
MessageBox.Show("Mgr" & dr(3))
MessageBox.Show("HireDate" & dr(4))
'displaying data from the table
End While
dr.Close()
myConnection.Close()
Catch ee As Exception
End Try
And i get the error on the Catch ee As Exception line: ORA-12560: TNS:protocol adapter error
我在 Catch ee As Exception 行上收到错误:ORA-12560: TNS:protocol adapter error
I also have a tnsnames.ora file on my computer but i am unsure if i need to use that when connecting (or really, how too in the first place)? Is it needed for the code above?
我的计算机上也有一个 tnsnames.ora 文件,但我不确定在连接时是否需要使用它(或者真的,首先如何使用)?上面的代码需要吗?
I am trying to use a DNS-Less connection to the DB. Not sure if that is what it is doing in this or not?
我正在尝试使用与数据库的无 DNS 连接。不确定这是否是它在做什么?
Any help would be great!!! :o)
任何帮助都会很棒!!!:o)
David
大卫
回答by Keeper
There are many ways: the one I use almost every time that doesn't require an entry in TNSNAMES.ORA is this:
有很多方法:我几乎每次都使用的一种不需要在 TNSNAMES.ORA 中输入的方法是:
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
And if you don't need an OleDb connection I think you should use System.Data.OracleClient or any other free provider (like DevArt dotConnect for Oracle Express)
如果您不需要 OleDb 连接,我认为您应该使用 System.Data.OracleClient 或任何其他免费提供程序(例如DevArt dotConnect for Oracle Express)
回答by Roman Podlinov
I always use www.connectionstrings.com/when I need to create a new connection string to the DB and when connection string format is not on top of my head.
当我需要为数据库创建一个新的连接字符串并且连接字符串格式不在我的脑海中时,我总是使用www.connectionstrings.com/。