I'm beginner in oracle,want to connect oracle database with c# windows appliation
but why i try connect database i get this error:
my listener file is this:
MYLISTNER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = DESKTOP-A5CFJSH)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_MYLISTNER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = C:\app\BEHZAD-HUSH\product\11.2.0\dbhome_2)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:C:\app\BEHZAD-HUSH\product\11.2.0\dbhome_2\bin\oraclr11.dll")
)
)
ADR_BASE_MYLISTNER = C:\app\BEHZAD-HUSH
and my tnsnames file is:
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)
What happen?How can i solve that?thanks.
my tns ping is:
TNS Ping Utility for 64-bit Windows: Version 11.2.0.1.0 - Production on 02-NOV-2015 15:42:22
Copyright (c) 1997, 2010, Oracle. All rights reserved.
Used parameter files:
C:\app\BEHZAD-HUSH\product\11.2.0\dbhome_2\network\admin\sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))) (CONNECT_DATA = (SID = CLRExtProc) (PRESENTATION = RO)))
TNS-12541: TNS:no listener
Server name should be from TNS (ORACLR_CONNECTION_DATA), not localhost.
EDIT:
TNS-12541: TNS:no listener
Check that tns listener is running.
CMD> lsnrctl
LSNRCTL> start
Before you start to develop application, you must first check database connection over tnsping/sqlplus. Only after you have successfully connected you can move forward.
If listener is curently runnig, you cant try to connect over tcp/ip, not over IPC (after changing tns connection string in tnsnames.ora) on this -
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP) (HOST = DESKTOP-A5CFJSH) (PORT = 1521))
(CONNECT_DATA = (SID = CLRExtProc))
)
TNS is a real double-edged sword. When it works, it works great, but if you don't have any control over the client machine where you are deploying an app, you may want to just skip it and connect directly.
Fortunately, Oracle came up with ezConnect, which dramatically simplifies connection strings. It's essentially:
server/port:service name (or SID)
By explicitly specifying the elements normally encapsulated in TNSNAMEs, you eliminate any chances that the target machine has a different definition for that TNS name or worse, no definition at all.
Or, if you use the Direct=true, you can still bypass TNSnames and explicitly spell out the server, port and service name:
String conString = String.Format("Direct=true;Server={0};" +
"Port={1};Service Name={2};User Id={3};Password={4};",
"myhost.foo.bar", 1521, "oraprod", "scott", "tiger");
or using the SID (I can never remember which works, so I try both):
String conString = String.Format("Direct=true;Server={0};" +
"Port={1};SID={2};User Id={3};Password={4};",
"myhost.foo.bar", 1521, "oraprod", "scott", "tiger");
and then:
OracleConnection conn = new OracleConnection(constring);
conn.Open();
(with appropriate error trapping, of course).
That was more info than you asked for, but the bottom line is when you are establishing the connection, I would use this as the "Server Name" in your dialog:
DESKTOP-A5CFJSH:1521/CLRExtProc
Related
Want to restore a database backup from linked server A to a database located on linked-server B using C#. prefer to use SMO.
I can restore from my local backup to local machine.
{
conn = new ServerConnection
{
ConnectionString = #"Data Source =
(localdb)\MSSQLLocalDb;Initial Catalog=master;Integrated Security=true",
};
try
{
//Restore Full
srv = new Server(conn);
//lsrv = srv.LinkedServers[#"DEVSQL\ALPHA"]; need to figure out how to restore to linked server instead of local.
//srv.KillAllProcesses("G4TestNew");
var res = new Restore();
res.Database = "G4TestNew";
res.Action = RestoreActionType.Database;
filePath = #"\\CABCSERVER\Database\Temp\Full.bak";
res.Devices.AddDevice(filePath, DeviceType.File);
res.ReplaceDatabase = true;
res.NoRecovery = true;
var dataFile = new RelocateFile("G4Test", #"C:\TBD\G4Test.mdf");
var logFile = new RelocateFile("G4Test_log", #"C:\TBD\G4TestNew.ldf");
res.RelocateFiles.Add(dataFile);
res.RelocateFiles.Add(logFile);
res.SqlRestore(srv);
}
EDIT(Adding more detail):.
In this case the linked servers are accessed via 'sql server authentication' and application does not have access to the credential required to connect directly and can use 'Integrated Security' to connect to localdb only.
In SMO you would not connect to one server and then administer a linked server. Instead connect directly to the target server. eg:
ConnectionString = #"Data Source =
DEVSQL\ALPHA;Initial Catalog=master;Integrated Security=true",
srv = new Server(conn);
I am Trying to Connect MY Sap B1 HANA on C# Web Based Application using DI API but my connection is giving me error. Here is Error Screenshot Failed to Connect SLD,make Sure Your SLD Server is Available and Connected. Any Relevant Help would be Appreciated.
try{
oCompany.CompanyDB = "***";
oCompany.Server = "***";
oCompany.LicenseServer = "***:30015";
oCompany.SLDServer = "***:40000"; //
oCompany.DbUserName = "****"; //
oCompany.DbPassword = "****"; //
oCompany.UserName = "****"; //
oCompany.Password = "****"; //
oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_HANADB;
oCompany.UseTrusted = false;
int res = oCompany.Connect();
string errMsg = oCompany.GetLastErrorDescription();
int ErrNo = oCompany.GetLastErrorCode();
if (ErrNo != 0)
{
value1 = errMsg;
return errMsg;
}
else {
value1 = "Succes Connection To Sap B1 Hana";
return value1;
}
You must include the port number in the server. Usually, the port number is 30015.
you can also use below mention code.
SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();
oCompany = (SAPbobsCOM.Company)Application.SBO_Application.Company.GetDICompany();
The following connection code should get you a step further:
// The actual database host
// With HANA the single-tenancy port 30015 needs to be provided together with the host (not so with MSSQL)
// When using HANA multi-tenancy the instance is prefixed and the port changed: INSTANCE#DB-HOST:30013
// OR the correct instance port needs to be provided, eg. DB-HOST:30017
sboCompany.Server = "DB-HOST:30015";
// The company database/schema name
// With MSSQL the instance is provided here like: INSTANCE\DB_NAME
sboCompany.CompanyDB = "SCHEMA";
// SLDServer is the new LicenseServer, don't forget the port with HANA
// Be aware: use either hostname or IP of SLD everywhere
sboCompany.SLDServer = "SLD-HOST:40000";
// Hell knows why the version needs to be provided for MSSQL...
sboCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_HANADB;
// DB credentials when using MSSQL authentication or HANA
sboCompany.UseTrusted = false;
sboCompany.DbUserName = "SYSTEM";
sboCompany.DbPassword = "password";
// SBO credentials
sboCompany.UserName = "manager";
sboCompany.Password = "password";
The next problems might be missing or wrong HANA drivers... your journey just began ;-)
I have 32-bit drivers installed on my box (they were installed and configured by some DBAs)
I wrote a simple script to test the drivers which pretty much is as follows
using (DataTable table = new DataTable())
{
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = "Data Source=alias;User id=user;Password=password";
connection.Open();
using (OracleCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM DUAL";
table.Load(command.ExecuteReader());
}
}
}
When I compile this application as 32-bit with the 32-bit Oracle.DataAccess.dll, it executes without a hitch.
However if I compile the application as AnyCPU with the Oracle.ManagedDataAccess.dll, i get an ORA-12154 (could not resolve the connect identifier specified) error.
If I tnsping alias, it works correctly and tells me the connect identifier with the real database name.
If I then change the connection string to use the real database name instead of the alias, and then try with the managed library again, it executes without a hitch.
I've been reading around and found this answer which says the managed driver relies on the tnsnames.ora file to resolve the aliases, however I rely on LDAP servers defined in sqlnet.ora and ldap.ora.
When I tnsping, it says it uses sqlnet.ora to resolve the name.
So how come the managed drivers do not work?
May this workaround is suitable for you. You can query the LDAP server by your own and put the full connection string to your code.
You can resolve the connection string from LDAP with this code:
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = "Data Source=" + ResolveServiceNameLdap("alias") + ";User id=user;Password=password";
connection.Open();
}
...
private static string ResolveServiceNameLdap(string serviceName)
{
string tnsAdminPath = Path.Combine(#"C:\oracle\network", "ldap.ora");
// or something more advanced...
// ldap.ora can contain many LDAP servers
IEnumerable<string> directoryServers = null;
if ( File.Exists(tnsAdminPath) ) {
string defaultAdminContext = string.Empty;
using ( var sr = File.OpenText(tnsAdminPath) ) {
string line;
while ( ( line = sr.ReadLine() ) != null ) {
// Ignore comments or empty lines
if ( line.StartsWith("#") || line == string.Empty )
continue;
// If line starts with DEFAULT_ADMIN_CONTEXT then get its value
if ( line.StartsWith("DEFAULT_ADMIN_CONTEXT") )
defaultAdminContext = line.Substring(line.IndexOf('=') + 1).Trim(new[] { '\"', ' ' });
// If line starts with DIRECTORY_SERVERS then get its value
if ( line.StartsWith("DIRECTORY_SERVERS") ) {
string[] serversPorts = line.Substring(line.IndexOf('=') + 1).Trim(new[] { '(', ')', ' ' }).Split(',');
directoryServers = serversPorts.SelectMany(x => {
// If the server includes multiple port numbers, this needs to be handled
string[] serverPorts = x.Split(':');
if ( serverPorts.Count() > 1 )
return serverPorts.Skip(1).Select(y => string.Format("{0}:{1}", serverPorts.First(), y));
return new[] { x };
});
}
}
}
// Iterate through each LDAP server, and try to connect
foreach ( string directoryServer in directoryServers ) {
// Try to connect to LDAP server with using default admin contact
try {
var directoryEntry = new DirectoryEntry("LDAP://" + directoryServer + "/" + defaultAdminContext, null, null, AuthenticationTypes.Anonymous);
var directorySearcher = new DirectorySearcher(directoryEntry, "(&(objectclass=orclNetService)(cn=" + serviceName + "))", new[] { "orclnetdescstring" }, SearchScope.Subtree);
SearchResult searchResult = directorySearcher.FindOne();
var value = searchResult.Properties["orclnetdescstring"][0] as byte[];
if ( value != null )
connectionString = Encoding.Default.GetString(value);
// If the connection was successful, then not necessary to try other LDAP servers
break;
} catch {
// If the connection to LDAP server not successful, try to connect to the next LDAP server
continue;
}
}
// If casting was not successful, or not found any TNS value, then result is an error
if ( string.IsNullOrEmpty(connectionString) )
throw new Exception("TNS value not found in LDAP");
} else {
// If ldap.ora doesn't exist, then throw error
throw new FileNotFoundException("ldap.ora not found");
}
return connectionString;
}
ODP.NET Managed driver relies on TNS_ADMIN value being set in the config file to find the TNSNAMES.ORA file. If you don't want to set that value, you can include the TNSNAMES.ORA file in the working directory of the executable or create a TNS alias in the config file directly.
Edit: If you are relying on SQLNET.ORA and LDAP.ORA you can also copy those into the working directory as well, or set the LDAP parameters in your config file. See the ODP.NET doc for the config file parameters available for LDAP.
I'm trying to open an OracleConnection on Visual Studio (c#) to join a Database with this command:
OracleConnection conn = new OracleConnection("Data Source=((DESCRIPTION=(ADDRESS_LIST =(ADDRESS=(PROTOCOL=tcp)(HOST=XX.XXX.X.XXX)(PORT=XXXX)))(CONNECT_DATA=(SERVICE_NAME=DEPL_D)(SERVER=xxxxx0xx)));User Id=X_XXX_XXX; Password=XXXXXXXXXX");
conn.Open();
my tnsnames.Ora contains the connection string like this:
DEPL_D.WORLD=
(DESCRIPTION=
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL = tcp)
(HOST = XX.XXX.X.XXX)
(PORT = XXXX)
)
)
(CONNECT_DATA =
(SERVICE_NAME = DEPL_D)
(SERVER = XXXXXXXXXX)
)
)
On the conn.Open(); I have the message ORA-12533: TNS: illegual ADDRESS parameter
After having searched on the net, It seems it's a problem on my tnsnames.oRA
I have done a test SQL+ with the administrator of the database (on audio) and it works. But It doesn't with visual studio.
If anybody has any idea, everything can helps.
Thanks in advance,
Greetings,
Flo
EDIT1:
I have tried to edit the (CONNECT_DATA =
(SERVICE_NAME = DEPL_D)
(SERVER = XXXXXXXXXX)
) with (CONNECT_DATA =
(SID=DEPL_D) but it doesn't works too.
Guessing from what you've provided: The SERVICE_NAME in the connect string is not the same SERVICE_NAME from your tnsnames.ora file.
Also, why did you censor the SERVER parameter ? Only possible values are SHARED, DEDICATED or POOLED. No ip address or hostname here.
I knew, there are thousands of topics similar to mine. I read them all. Nothing works for me. And there I will describe my problem: I created a Oracle database using Oracle SQL developer setting values:
connection name: test,
username: bob,
password: qwerty
Connection type: basic,
role: default,
Hostname: localhost,
port: 1521,
SID : orcl
Reason I'm writing all of this is that I really did everything possible, and I hope that by providing each kind of data someone will be able to help me.
My next step was downloading and installing ODAC 11.2 Release 4 (11.2.0.3.0) with Oracle Developer Tools for Visual Studio.
When I want make a connection to database from my application using server explorer I'm entering following data:
data source name: //localhost:1521/test
user name: ADMIN
password : qwerty
connection name is being set automatically.
When I'm trying to test connection I'm getting ORA 12514... and here it is how I changed my tnsname.ora:
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
orcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
Make sure of the following:
Oracle service is running on windows.
Your pfile or spfile has local_listener='LISTENER_ORCL'
change the orcl config in tnsname.ora as :
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)