ORA-01005 error connecting with ODP.Net - c#

I am trying to access an Oracle database (version 10.2.0.4.0) using the following code but an "ORA-01005: Null password given; logon denied" exception is raised by the connection when it's open method is called.
var connBuilder = new OracleConnectionStringBuilder();
connBuilder.DataSource = "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = MyHost.Address)(PORT = ####)) )(CONNECT_DATA =(SERVICE_NAME = MyService)))";
connBuilder.UserID = "validUserId";
connBuilder.Password = "validPassword";
connBuilder.PersistSecurityInfo = true;
var connString = connBuilder.ToString();
using (var con = new OracleConnection(connString))
{
con.Open();
}
If I change the username then I receive the following instead; "ORA-01017: invalid username/password; logon denied" and this is also the case if I change the open call on the connection with con.OpenWithNewPassword("validPassword");
If I try with the deprecated Oracle client it connects with no problems:
using (var depCon = new System.Data.OracleClient.OracleConnection
("Data Source=MyHost.Address:####/MyService;Persist Security Info=True;
User ID=validUsername;Password=validPassword;Unicode=True"))
{
depCon.Open();
}
I'd (obviously) like to use the latest Odp.Net drivers but can't seem to get past this issue. Has anybody got any ideas?

Take a look at this thread for an issue regarding FIPS compliance:
https://community.oracle.com/thread/2557592?start=0&tstart=0
Also:
Oracle.ManagedDataAccess and ORA-01017: invalid username/password; logon denied

Does it work when you do it like this:
var connBuilder = new Common.DbConnectionStringBuilder();
connBuilder.Add("Data Source", "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = MyHost.Address)(PORT = ####)) )(CONNECT_DATA =(SERVICE_NAME = MyService)))";
connBuilder.Add("User Id", "validUserId");
connBuilder.Add("Password", "validPassword");
Which version of ODP.NET do you use? There are known issues when you connect to a "new" Oracle database with case-sensitive passwords using an "old" ODP.NET provider, see here: https://community.oracle.com/message/2198228
In order to verify run this command on your database:
ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON = FALSE;

The issue with case sensitivity and the ODP.Net drivers and different DB versions should be a simple fix. Enclose your connectionstring password in quotes(") such as Password=\"password\"; and this should keep the password case

Related

SQL Exception - Network-related or instance specific. SQL Express "It works on my machine" issue

EDIT: It only took a week but I eventually found out the issue, primarily due to pure luck and another error with a more specific fix.
The issue was with the connStr I had made, which for some reason on this machine gave me the error randomly of "System.ArgumentException: Keyword not supported: 'datasource'." during runtime. I then found out a fix for that was to rename the connStr as follows:
connStr = #"server = (server name); Initial Catalog = AutoTestDB; Integrated Security = true";
Should you have this error as I have, try that method of connection. END EDIT
I'm currently working on Automated Testing using Katalon Automated Testing, essentially Selenium for Chrome, and whenever I'm attempting to add the results of the test to our Test Results Database the SQL Exception "A network-related or instance-specific error occurred while establishing a connection to SQL Server. " keeps popping up. TCP/IP is open, as is firewall and remote connections, and I have the SQL-SMS open and running while I run the database with the SQL connection.
However it only happens whenever I'm using a certain machine to access the database which is stored within the machine itself, as it is with every other machine that I use and they all work perfectly fine. The only difference I can think of for this machine is that it uses SQL Express while all the others that I use have the full version of Microsoft SQL-SMS-17.
It's a genuine case of "It works on my machine", except with the caveat that it works on several others and even across different users as we are all working on this automated testing, this machine is the lone exception for this code not working, with the only difference being that it uses SQL Express which should be accounted for with the \\SQLExpress.
C# code with SQL connetions to edit the values into an already made table within the database.
public void testDBAdd(String testName, Boolean pass, String testComment)
{
SqlConnection con;
SqlDataAdapter daAutoTest;
DataSet dsAutoTestDB = new DataSet();
SqlCommandBuilder cmdBAutoTest;
String connStr, sqlAutoTest;
connStr = #"datasource = .\\sqlexpress; Initial Catalog = AutoTestDB; Integrated Security = true";
con = new SqlConnection(connStr);
sqlAutoTest = #"SELECT * FROM TestResults";
daAutoTest = new SqlDataAdapter(sqlAutoTest, connStr);
cmdBAutoTest = new SqlCommandBuilder(daAutoTest);
daAutoTest.FillSchema(dsAutoTestDB, SchemaType.Source, "AutoTest");
daAutoTest.Fill(dsAutoTestDB, "AutoTest");
foreach (DataRow drAutoTest in dsAutoTestDB.Tables["AutoTest"].Rows)
{
if (pass == true && drAutoTest["testName"].ToString() == testName)
{
drAutoTest.BeginEdit();
drAutoTest["testName"] = testName;
drAutoTest["testResult"] = 1;
drAutoTest["testComment"] = testComment;
drAutoTest.EndEdit();
daAutoTest.Update(dsAutoTestDB, "AutoTest");
}
else if (pass == false && drAutoTest["testName"].ToString() == testName)
{
drAutoTest.BeginEdit();
drAutoTest["testName"] = testName;
drAutoTest["testResult"] = 0;
drAutoTest["testComment"] = "Exception: " + testComment;
drAutoTest.EndEdit();
daAutoTest.Update(dsAutoTestDB, "AutoTest");
}
}
}
Code which runs the actual test and gathers if it has passed or failed due to the presence of certain elements, in this case is a certain page displayed when the user logs in and clicks a button.
public void settingTest<TestNumber>()
{
IWebDriver driver = new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start-maximized");
driver = new ChromeDriver(options);
String testName = "<Test Number>", testComment = "";
Boolean pass = false;
try
{
settingsLogin(driver);
settingsClick(driver);
Assert.IsTrue(driver.FindElement(ElementLocator).Displayed);
if (driver.FindElement(ElementLocator).Displayed == true)
{
testComment = "Pass";
pass = true;
testDBAdd(testName, pass, testComment);
}
}
catch (Exception ex)
{
testComment = "" + ex.TargetSite + "" + ex.Message;
testDBAdd(testName, pass, testComment);
}
finally
{
driver.Close();
}
}
Not sure, but I think your connection string has an extraneous backslash. You've prefaced the string with a "#" but then used "\\" in the Data Source. You might also try "(localdb)\SQLExpress" as the data source.
It only took a week but I eventually found out the issue, primarily due to pure luck and another error with a more specific fix. The issue was with the connStr I had made, which for some reason on this machine gave me the error randomly of "System.ArgumentException: Keyword not supported: 'datasource'." during runtime. I then found out a fix for that was to rename the connStr as follows:
connStr = #"server = (server name); Initial Catalog = AutoTestDB; Integrated Security = true";
Should you have this error as I have, try that method of connection. And thanks to the users who tried to help in both the comments of the post and in the answers section of this post.

Connecting to an Oracle Database in a C# Windows application with ManagedDataAccess using Bequeath Protocol

I'm attempting to use a Visual Studio C# Windows Application that connects to a local Oracle 12.2 database (using Oracle.ManagedDataAccess.Client) using the Bequeath protocol so that I don't have to go through the Oracle listener since this is just for a local database connection.
I know that sqlplus natively uses the Bequeath protocol when not specifying a connect string or TNS entry:
sqlplus scott/tiger
*connected*
The SID and service_names are both "mydb":
select instance from v$thread;
-----
mydb
show parameter service_names;
VALUE
-----
mydb
I can also use the Bequeath protocol when connecting through SQL Developer (using the connection type of Local/Bequeath).
However, I have been unable to find an example of using Bequeath in a connection string that works (at least for me).
The 12.1 documentation for Oracle Net Services isn't really helpful. It gives an overview of Bequeath without any examples.
tnsnames.ora:
MYDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SID = MYDB)
)
)
MYDB_BEQ =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = BEQ)(PROGRAM = oracle)(ARGV0 = oracleMYDB)
)
)
(CONNECT_DATA = (SID = MYDB)
)
)
The following works using TCP (which goes through the listener):
new OracleConnection(#"User Id=scott; Password=tiger
;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(
HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)
(SERVICE_NAME=MYDB)))");
*connected*
Or using the TNSNAMES entry:
new OracleConnection("User Id=scott;Password=tiger;Data Source=MYDB")
*connected*
I can connect through TNSNAMES using the above BEQ entry using SQLPLUS:
sqlplus scott/tiger#mydb_beq
*connected*
However, I can't seem to get this to work through Oracle.ManagedDataAccess:
new OracleConnection(#"User Id=scott; Password=tiger
;Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = BEQ)(PROGRAM = oracle)(ARGV0 = oracleMYDB)))(CONNECT_DATA = (SID = MYDB)))")
ORA-12533: Network Session: Connect transport address syntax error
Also tried this to use the TNSNAMES entry that worked above:
new OracleConnection("User Id=scott;Password=tiger;Data Source=MYDB_BEQ")
ORA-12533: Network Session: Connect transport address syntax error
I'm probably missing something basic here, but i'm not sure what else to try...
It doesn't look like that is possible.
In the dataSources Section of the 12c Data Provider for .NET Developer's Guide:
No bequeath (beq) support. Default address is instead TCP loopback
with port 1521 and Oracle service name from environment (ORACLE_SID)
This solved the problem
options.UseOracle(#"User Id = test; Password = 123456; Data Source =LOCALHOST:1521/ORCL.DLINK;");

Connecting to mysql on 000webhost using C#

Im simply just trying to read what there is in the batabase on to a console but i always get an exception on the conn.Open() line. Here is all the code:
SqlConnectionStringBuilder conn_string = new SqlConnectionStringBuilder();
conn_string.DataSource = "mysql14.000webhost.com"; // Server
conn_string.UserID = "a7709578_codecal";
conn_string.Password = "xxxxx";
conn_string.InitialCatalog = "a7709578_codecal"; // Database name
SqlConnection conn = new SqlConnection(conn_string.ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("Select name FROM Users");
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{1}, {0}", reader.GetString(0), reader.GetString(1));
}
reader.Close();
conn.Close();
if (Debugger.IsAttached)
{
Console.ReadLine();
}
You need to build the connection string manually or use MySqlConnectionStringBuilder. MySql uses a different format than SQL Server and the SqlConnectionStringBuilder that you're using. You also need to use a MySQL library, SqlConnection, SqlCommand, etc are all build specifically for SQL Server.
MySQL connectors
For MySQL database you are using wrong provider. Those classes you have used in posted code are for SQL Server. Your code should look like below with MySQL provider related classes
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "mysql14.000webhost.com";
conn_string.UserID = "a7709578_codecal";
conn_string.Password = "xxxxxxx";
conn_string.Database = "a7709578_codecal";
using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
Check Related post in SO
Also to point out, you are selecting only one column from your table as can be seen
new SqlCommand("Select name FROM Users");
Whereas trying to retrieve two column value, which is not correct
Console.WriteLine("{1}, {0}", reader.GetString(0), reader.GetString(1))
000webhost free servers does not allow external connections to the server database.
You can only use your database from your PHP scripts stored on the server.
You can get data from database using PHP and it will return.So i advice to you using php from C# like api.

No start database manager command was issued error

I have a DB2 expresss in my machine and I am able to query from the database using command window (after following two commands):
set DB2INSTANCE=db2inst1
db2 connect to tims user
Now, when I try to connect to the database from a C# console application, I am getting following errors with different connection strings.
Attempt 1
string connectionString = #"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; CurrentSchema=db2inst1; ";
SQL1032N No start database manager command was issued. SQLSTATE=57019
Attempt 2
string connectionString = #"Provider = IBMDADB2; Database = TIMS; CurrentSchema=db2inst1; ";
SQL1031N The database directory cannot be found on the indicated file system. SQLSTATE=58031
What should be the correct connection string for this scenario?
CODE
string connectionString = #"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; CurrentSchema=db2inst1; ";
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = connectionString;
myConnection.Open();
Do you have multiple DB2 instances running on your machine? You can get a list of instances that exist by executing the db2ilist command.
If you have to execute the set DB2INSTANCE=db2inst1 statement when you open a DB2 Command Window in order to connect to the TIMS database with the db2 connect to TIMS command, then you need to ensure that the environment for your C# application is configured the same way.
You can do this in a number of ways:
by setting the DB2INSTANCE environment variable before starting your application
Change the default DB2 instance on your machine by using the command db2set -g DB2INSTDEF=db2inst1 (** see note below)
Use a TCPIP connection string (as described by #Bhaarat) so that your application does not depend on the database catalog for the default instance
Note: Before changing DB2INSTDEF you may want to see what the current value is, by executing the command db2set -all and looking for DB2INSTDEF in the output. Also note that changing the default instance may affect other applications that run on your machine.
refer this url http://www.c-sharpcorner.com/uploadfile/nipuntomar/connection-strings-for-ibm-db2/
your connection string should be something like this
Provider=IBMDADB2;Database=urDataBase;Hostname=urServerAddress;Protocol=TCPIP;Port=50000;
Uid=urUsername;Pwd=urPassword;
in more you can refer this too
http://www.codeproject.com/Articles/4870/Connect-to-DB2-from-Microsoft-NET
My DB2 insatnce name is "db2inst1" and it was working fine when I used DB2 command window.
Now I made following settings and it is working fine now. :-)
Created a port in the C:\Windows\System32\drivers\etc\services file (db2c_db2inst1 50010/tcp)
Set the “TCP/IP Service Name” ( = db2c_db2inst1”) for the instance. Verified using “db2 get dbm cfg” command
Updated the environment variable DB2INSTANCE with the value “db2inst1”
Restarted the machine
Started the instance
ConnectionString
"Provider = IBMDADB2; Database = TIMS; Hostname = localhost; Protocol = TCPIP; Port = 50010; Uid = myUserID; Pwd = myPassword";
CODE
string queryString = "SELECT * FROM DBATABC";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
string companyCode = reader.GetString(0).ToString();
}
}
}
reader.Close();
}
}
Note: Try to create DSN for ODBC and use ODBC connection in a sample SSIS package. This will help in resolving OLEDB connection issues also (that are common to both)

is there anyway to connect to sybase from c# without having to add a ODBC DSN connection in control panel

I am looking for a way to connect to sybase from C# without having to setup an ODBC DSN connection locally on the machine.
Is this possible? I tried all of these different connection strings:
static private DataTable Run(string sql)
{
var conn = new OdbcConnection();
const string CONN_STRING2 =
"Data Source='[myServer]';Port=5020;Database=[dbName];Uid=[user];Pwd=[pwd];";
const string CONN_STRING1 =
"Provider=Sybase.ASEOLEDBProvider.2;Server Name=[myServer];Server Port Address=5020;Initial Catalog=[dbName];User ID=[user];Password=[pwd];";
conn.Open();
var da = new OdbcDataAdapter { SelectCommand = conn.CreateCommand() };
da.SelectCommand.CommandText = sql;
var dt = new DataTable();
da.Fill(dt);
da.Dispose();
conn.Close();
return dt;
}
But I got an error stating:
{"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"}
You are using var conn = new OdbcConnection(). That is why you are getting the error.
You will need to use a sybase oledb client library. I don't know sybase at all but last I heard one needed to purchase a third-party provider. This was almost three years ago so they may have one by now.
You would then need to do something like var connection = new ASEConnection().
Download the DLL from the following link:
Sybase library
Use this library for connection:
ASEconnection connection = new AseConnection();
Use specific ADO.Net data provider stright from manufacturer (SAP now own sybase). I think you can manage to create a connection string yourself using this reference. For example:
var connStr = #"Data Source=\\myserver\myvolume\mypat\mydd.add;User ID=myUsername;
Password=myPassword;ServerType=REMOTE;"
using (ASEConnection conn = new AseConnection(connStr)) {
// use conn
}
Same informaton can be obtained from documentation mentioned earlier.

Categories