I've configured my Oracle Database for NTS Authentication and set my Windows Login up as a user in the database.
I'm able to log in to the Database with the command sqlplus /.
I'm also able to connect using Windows Authentication using the System.Data.OracleClient provider for ADO.NET.
For example, the following code snippet works:
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OracleClient");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = "Data Source=//localhost/Test; Integrated Security=yes";
connection.Open();
However, I'm unable to connect using Windows Authentication using Oracle.DataAccess.Client (ODP.NET).
DbProviderFactory factory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = "Data Source=//localhost/Test; User Id=/";
connection.Open();
This block of code results in the following exception:
Oracle.DataAccess.Client.OracleException was unhandled
Message=ORA-1017: invalid username/password; logon denied
According to this link I should be able to create an ODP.NET connection to Oracle using the provided connection string: http://www.oracle.com/technetwork/articles/dotnet/cook-masteringdotnet-090821.html
Why does the ODP.NET client not allow me to connect while the (deprecated) Microsoft client does?
This problem was discussed at the following thread in 2007, but no solution was every provided: http://forums.oracle.com/forums/thread.jspa?messageID=2312148.
This is a showstopper.
It works fine for me using ODP.net 11 and this connection string (with a tnsnames.ora file in the Oracle client to define DLGP): Data Source=DLGP;User Id=/;Password=;
Your sqnet.ora file needs to be setup correctly for this to work, but that should already be the case if SQLPlus can connect. Do you only have one Oracle home? If ODP.net is picking up a second one that can muck things up nicely (and that can happen depending on how you install it).
Related
I am using Microsoft OLE DB Provider for DB2 V6 provider to connect to db2 database in my .NET 4.5.2 application using C# and this is my connection string:
Provider=DB2OLEDB.1;Password=xxx;Persist Security Info=True;User ID=myUser;Initial Catalog=DSNP;Data Source=DSNP;Network Address=10.0.202.145;Network Port=666;Default Schema=PPBANCS;Connect Timeout=10;
As you see I have set Connect Timeout to 10 seconds but the problem is notwithstanding I can see this value inside the OleDbConnection.ConnectionTimeout property but it does not work and OleDbConnection.open() waits for connection for ever. Also have this problem too with OleDbCommand.CommandTimeout.
private void Connect()
{
connection = new OleDbConnection(_connectionString);
connection.Open();
}
Any help would be greatly appreciated.
The server admin created a 32-bit ODBC system DSN for me that has the database and user credentials in it. I'm struggling now to understand how to connect to that database from my C# code. If I'm using an SSIS connection it goes through without issue, so I know the data they set is correct.
Am I supposed to be using SqlConnection or OleDbConnection to access this now? I've tried both, and no matter what type of connection strings I try it always results in errors. This is connecting to a Denodo instance of that matters for the connection string.
Just put in the DSN name that's been configured:
using System.Data.Odbc;
OdbcConnection DbConnection = new OdbcConnection("DSN=SAMPLE_ISAM");
// Your code here
DbConnection.Close();
Everything else is the same, the "Connection String" information is all contained in the DSN itself, if properly configured.
When I open SQL Command Line, I write CONNECT username/password#[//]host[:port][/service_name] and it connects me to the database just fine. However, I'm unable to connect from a .NET project using a connection string. I have tried many things such as <add name="ConnectionString" connectionString="Data Source=username/password#[//]host[:port][/service_name];" /> and <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />, but nothing worked so far. Whenever I get to sconn.Open(); in the following:
var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();
I practically always get the following error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 25 - Connection string is not valid)
How can I connect to the database properly?
Try following connection string
string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";
& then use this conection string as follow
using (OracleConnection objConn = new OracleConnection(con))
{
\\ code
}
I highly recommend using the "Official Oracle ODP.NET, Managed Driver".
https://www.nuget.org/packages/Oracle.ManagedDataAccess/
Or the one for Entity framework:
https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/
After installing via Visual Studio it opens a readme that I also recommend you read.
Oracle provide massive amounts of documentation. This is where to start:
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html
You use System.Data.SqlClient.SqlConnection which is used to connect to a (Microsoft) SQL Server, it cannot be used for an Oracle connection.
You should use System.Data.OracleClient.OracleConnection or Oracle.ManagedDataAccess.Client.OracleConnection
Have a look at this answer to see other possibilities:
How to connect to Oracle 11 database from . net
You can always use EF, and use create one ADO.Net Entity Data Model
And use the wizard to create and test the connection
How to connect the oracle database as a backend in asp.net (C#)?
what is the connection string for it?
when i was trying to connect i got the below error:
ORA-12154: TNS:could not resolve the connect identifier specified
The actual connection string depends on the parameters of your server (ip, instance name, credentials, etc).
Here's a site with several 'example' connection strings for oracle:
http://www.connectionstrings.com/oracle
To connect to Oracle DB with .NET, best way is:
Install Oracle client
Configure tnsNames.ora
Reference .net project to Oracle.DataAccess.Dll (ODP.NET)
Create connection using OracleConnection object
I will not even go into other possibilities because this is golden standard for .net-Ora connectivity
The following is a connection string generated when I connect to a database using a configuration tool with Microsoft OLE DB Provider for SQL Server.
Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=sa;
Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Use Encryption for Data=False;Tag with column collation when possible=False
If I connect to the same database but with SQL Server Native Client 10.0 I get this connection string.
Provider=SQLNCLI10.1;Integrated Security=\"\";Persist Security Info=False;
User ID=sa;Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Initial File Name=\"\";Use Encryption for Data=False;
Tag with column collation when possible=False;
MARS Connection=False;DataTypeCompatibility=0;Trust Server Certificate=False\0
I have a c# application that reads either one of these connection strings and uses it to create a connection to my database like so
SqlConnectionStringBuilder _sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
OleDbConnectionStringBuilder conBuilder = new OleDbConnectionStringBuilder( CONNECTION STRING SHOWN ABOVE);
_initialCatalogValue = (string)conBuilder["Initial Catalog"];
_dataSourceValue = conBuilder.DataSource;
_sqlConnectionStringBuilder.Password = (string)conBuilder["Password"];
_sqlConnectionStringBuilder.UserID = (string)conBuilder["User Id"];
_sqlConnectionStringBuilder.InitialCatalog = _initialCatalogValue;
_sqlConnectionStringBuilder.DataSource = _dataSourceValue;
_sqlConnectionStringBuilder.PersistSecurityInfo = true;
_conn = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
_conn.Open();
The problem that when I use the SQL Server native client the password is empty and my SQLConnection will fail on the login. The error I get is "Login failed for user 'sa'".
The OLE DB connection string is successful. Is my login failing for the sql server native client due to some of the classes I am using in c#? Does the Sql Server Native Client 10.0 encrypt the password? Should I try to identify which provider is in the connection string and have two different code paths? If so what would it take to connect?
Basic question is, how can I ensure a successful connection regardless of which connection string I receive (only the two above)?
N.B. I have no control over the connection strings. I can only work with what I am receiving.
The second connection string you provide does not include a password; thus conBuilder["Password"] returns an empty string when you set conBuilder.ConnectionString to the second string.
The problem was solved as follows.
There was a main application that was making use of the above connection strings and was doing the following.
The application would take the connection string.
If the connection string's provider is SQL Server Native Client (SQLNCLI10.1) the application checks for persistent security. If it cannot find any it adds IntegratedSecurity=SSPI to the connection string and then connects using windows authentication instead. Whether or not this is the right (or secure thing to do) that is what was being done.
To 'answer' the question. You can take in the connection string and if you do not find a password you can set IntegratedSecurity=SSPI. This will allow you to connect using windows authentication instead of SQL Server authentication. I am not advising that you do, but it will work.