Query Oracle using .net core throws exception for Connect Identifier - oracle

I'm quite new to Oracle and never used that before, now, I'm trying to query an Oracle database from a .Net Core Web Application having nuget package oracle.manageddataaccess.core installed and using an alias as the Data Source but I'm receiving the following error:
If I use the full connection string the query will work correctly
ORA-12154: TNS:could not resolve the connect identifier specified
at OracleInternal.Network.AddressResolution..ctor(String TNSAlias, SqlNetOraConfig SNOConfig, Hashtable ObTnsHT, String instanceName, ConnectionOption CO)
at OracleInternal.Network.OracleCommunication.Resolve(String tnsAlias, ConnectionOption& CO)
at OracleInternal.ConnectionPool.PoolManager`3.ResolveTnsAlias(ConnectionString cs, Object OC)
at OracleInternal.ServiceObjects.OracleConnectionImpl.Connect(ConnectionString cs, Boolean bOpenEndUserSession, OracleConnection connRefForCriteria, String instanceName)
So, from a few links I could understand that there is a tsnnames.ora file which must contain the map between connect identifiers and connect descriptors. And that this file can be found at the machine on which the Oracle has been installed with the path ORACLE_HOME\network\admin.
Question is:
Does the alias name that I'm using in my connection string which reads as Data Source: <alias_name>; User ID=<user>; Password=<password> need to be specified in the tsnnames.ora file? I don't have access to the machine where the Oracle database resides on otherwise I would have checked it earlier.
Here's the code snippet for more info: connection string and query values are mocked out
public static string Read()
{
const string connectionString = "Data Source=TestData;User ID=User1;Password=Pass1";
const string query = "select xyz from myTable";
string result;
using (var connection = new OracleConnection(connectionString))
{
try
{
connection.Open();
var command = new OracleCommand(query) { Connection = connection, CommandType = CommandType.Text };
OracleDataReader reader = command.ExecuteReader();
reader.Read();
result = reader.GetString(0);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
return result;
}
Is this enough or something else needs to be added/changed in here? Or probably the issue is with the tsnNames.ora file which might not contain the alias name here?
Thanks in advance

For the Data source
Data Source=TestData;User Id=myUsername;Password=myPassword;
Your tnsnames.ora probably should have the below entry
TestData=(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost) (PORT=MyPort)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)))
Since
Data Source=TestData;User Id=myUsername;Password=myPassword;
is same as
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost) (PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;

Related

How to check for SQLite database file existence in Xamarin and create a new database file if there were no such a database?

I'm trying to write a method for checking SQLite file existence. I downloaded a Nuget package for SQLite functionalities named System.Data.SQLite.Core.
I wrote this code for the method:
string path = "ReportData.db";
private void Create_db()
{
if (!System.IO.File.Exists(path))
{
SQLiteConnection.CreateFile(path);
using (var sqlite = new SQLiteConnection(#"Data Source=" + path))
{
sqlite.Open();
string sql = "create table temp(date varchar(20),activity varchar(1000))";
SQLiteCommand command = new SQLiteCommand(sql, sqlite);
command.ExecuteNonQuery();
sqlite.Close();
}
}
}
I don't know where to create my SQLite database file. When I use this method in a button, the application goes into break mode and says:
System.UnauthorizedAccessException: 'Access to the path
"/ReportData.db" is denied.'
Please help me.

Not able to connect to Oracle DB through Wallet from C#

I am trying to connect to the Vendor's database through the Wallet. For that, the vendor has provided a wallet zip files which contains two files (one .ora file and one .sso file) . The zip file contains tnsnames.ora and cwallet.sso
First, I installed the Oracle client version 19.0 Home1 which has created a file called sqlnet.ora automatically. I copied the file tnsnames.ora to the same folder where sqlnet.ora created, and tried different wasys to connect the database, but every attempt failed. I tried to implement in similar way which is explained in this link, but getting errors that "TNS:Lost contact", "TNS:Could not resolved the connect identifier specified".
I tried the solution which specified here, but ended up in error. Finally, the error now is "Connection failed because target host or object does not exist". The connection details are specified in tnsnames. ora that contains the service name and host name. I have added the Wallet location in sqlnet.ora file and also the following code:
SSL_CLIENT_AUTHENTICATION = FALSE
SSL_VERSION = 1.2
SSL_CIPHER_SUITES = (SSL_RSA_WITH_AES_256_CBC_SHA)
SSL_SERVER_DN_MATCH = ON
The connection code in C# is below:
//string strCon = "User Id =XXXX;Password=XXXXXXX;Data Source=(DESCRIPTION =(ADDRESS=(PROTOCOL=TCP)(HOST=XXXXXXXX)(PORT=2460))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XXXX))" +
// "(SECURITY=(SSL_SERVER_CERT_DN='C=US,ST=Washington,L=Seattle,O=Amazon.com,OU=RDS,CN=mcsldb01.ckc6ued9hacf.ap-south1.rds.amazonaws.com')));";
using (OracleConnection con = new OracleConnection(strCon))
//using (OracleConnection con = new OracleConnection(#"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXXXXXXX)(PORT=2460)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XXXX)));User Id=XXXX;Password=XXXXXXXX;"))
//using (OracleConnection con = new OracleConnection(cntn))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
//TNS_ADMIN and wallet directory entries can be entered in the app.config file.
con.Open();
Console.WriteLine("Successfully connected to Oracle Autonomous Database");
//Retrieve database version info
cmd.CommandText = "SELECT BANNER FROM V$VERSION";
OracleDataReader reader = cmd.ExecuteReader();
reader.Read();
Console.WriteLine("Connected to " + reader.GetString(0));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
Please suggest any solution. Where I am going wrong? Not able to proceed further.
At a guess, it might have been a connection wallet not an SSL wallet that has been provided, ie, just a mechanism of hiding the username and password. If that is the case, the connection would look something like:
new OracleConnection(#"Data Source=NAME_IN_TNSNAMES;User Id=/"))
where NAME_IN_TNSNAMES maps to the entry in the tnsnames.ora file.
Examples of that (from a SQL Plus perspective) here
https://connor-mcdonald.com/2015/09/21/connection-shortcuts-with-a-wallet/

Oracle database change notification with ODP.NET doesn't work

I'm complete newbie to Oracle DB trying to enable DB change notifications.
private void RegisterNotification()
{
const string connstring = "Data Source=ORA_DB;User Id=USER;Password=pass;";
try
{
var connObj = new OracleConnection(connstring);
connObj.Open();
var cmdObj = connObj.CreateCommand();
cmdObj.CommandText = "SELECT * FROM MYTABLE";
var dep = new OracleDependency(cmdObj);
dep.QueryBasedNotification = false;
dep.OnChange += new OnChangeEventHandler(OnNotificationReceived);
cmdObj.ExecuteNonQuery();
connObj.Close();
connObj.Dispose();
connObj = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public static void OnNotificationReceived(object src, OracleNotificationEventArgs arg)
{
MessageBox.Show("Table has changed!");
}
I've executed "GRANT CHANGE NOTIFICATION TO USER;" but nothing happens when I change the table data neither manually nor programmatically. Query-based notifications also don't work. I suppose I miss something in Oracle configuration.
I have Oracle 11.2 standard edition.
The CHANGE NOTIFICATION permission is not on the features of the Standard Edition for latest versions :
Licensing information
Oracle TimesTen Application-Tier Database Cache :
Data access using PL/SQL, JDBC, ODBC, ttClasses, OCI, and Pro*C/C++
interfaces Transaction Log API (XLA) for change notification
Multi-node Cache Grid
...
SE2 : N
EE : Y (extra-cost option)
Try to execute your soft under admin permission and as console application. when we used to deal with it we faced with the same stuff. we hadn't managed to use it in webservices.

How to read columns of SQL Server tables in Visual Studio?

In Microsoft Access functions like DLookup - DMax or Dcount help the programmer to read a column from a table of a SQL Server database.
How do you do the same task in Visual Studio?
For example how can I find the ID of a user (John) in tblUsers table.
tblUsers columns: ID, Username, Password, .....
I've already added the SQL Server database to the Data Source.
Any kind of advice is much appreciated.
ow. Even in Access, that isn't proper.
Go read up on DAO, ADO, and, if you're using Visual Studio to write .Net applications, the system.data namespace.
in general, when accessing relational data via non-database program code, including the vba you're using in Access, you'd retrieve a reference to a Recordset object and query each record's fields as object properties.
If you want to visualize all of the data, click Database Explorer (see the tab at the bottom of the solution explorer), then expand your data connections and right click the name of the table, then click 'show table data'. If you want only the id's or names etc, I usually just create an SQL script to run, and run it when I want to browse certain data.
To simply create a script, while you're viewing all the data, click the script button above the table your viewing. Then you can execute your script
SELECT * FROM tblUsers WHERE User = 'John';
I hope I've understood your questions correctly.
I followed DougM and ended up with the following code. I will add it here just in case somebody else has the same question:
using System.Data.SqlClient; should be added to the reference section
private void button1_Click(object sender, EventArgs e)
{
string fltr = "UserName='Ando'";
MessageBox.Show(ReadOrderData(fltr));
}
public string ReadOrderData(string filter)
{
string connectionString = "Data Source=sqlServer-servername;Initial Catalog=database name;Integrated Security=True";
string queryString = "SELECT User_ID, UserName FROM tblUsers WHERE " + filter + ";";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
return reader[0].ToString();
//Console.WriteLine(String.Format("{0, {1",
// reader[0], reader[1]));
}
}
finally
{
// Always call Close when done reading.
reader.Close();
}
}
return "Null";

SAP Business One: Connection Error When I try to connect to UI API

I got this error message
"Connection - Could not find SBO that match the connection string [66000-85]"
when I try to connect SAP Business One UI API.
I connect like the following :
private void SetApplication()
{
SAPbouiCOM.SboGuiApi SboGuiApi = null;
string sConnectionString = null;
SboGuiApi = new SAPbouiCOM.SboGuiApi();
// connect to a running SBO Application
sConnectionString = Environment.GetCommandLineArgs().GetValue(1).ToString() ;
SboGuiApi.Connect(sConnectionString);
SBO_Application = SboGuiApi.GetApplication(-1);
}
I got it this problem, my connestring string is wrongly config. WHen I set rigth one it works now. Thanks all.

Resources