connect to oracle DB using TnsNames Alias - c#

In my TnsNamesOra I have
TEST11.12.13.14 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 11.12.13.14)(PORT = 1234))
)
(CONNECT_DATA =
(SERVICE_NAME = TESTNAME)
)
)
If my connection string looks like this:
Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.12.13.14)(PORT = 1234)))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = TEST))); User Id = admin; Password = admin ; DBA Privilege = SYSDBA ; Pooling = false; Connection Timeout = 30" providerName="Oracle.DataAccess.Client
Everything works fine.
But I want to do something like this:
Data Source = TEST11.12.13.14; User Id = admin; Password = admin ; DBA Privilege = SYSDBA ; Pooling = false; Connection Timeout = 30" providerName="Oracle.DataAccess.Client
But getting error
ORA-12154: TNS:could not resolve the connect identifier specified

Related

Can't figure how to fix an issue sending MariaDB query results as email in a C# console app

All it does is send me an email with everything except the body. So no results. What I am trying to do is to get the results back to my console app and then send the same results of the query as a table to specific email addresses. Newbie here. Any help would be very appreciated.
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.UserID = "PlaceHolder";
builder.Password = "PlaceHolder";
builder.ConnectionString = "http://PlaceHolder.com";
builder.Database = "PlaceHolder";
ConnectionString = builder.ConnectionString;
}
public bool GIS_ALERT(string pUserName)
{
bool success = false;
conn.Open();
using (MySqlConnection conn = new MySqlConnection { ConnectionString = ConnectionString })
{
using (MySqlCommand cmd = new MySqlCommand { Connection = conn })
{
cmd.CommandText = "SELECT* FROM StepData WHERE CLIENT = 'msaff' AND StepName = 'Canonical2FlatFile' AND DocumentName<> 'null' AND StepDate = CURRENT_DATE() AND StepTime BETWEEN CURRENT_TIME(3) -INTERVAL 2700 SECOND AND CURRENT_TIME(3) - INTERVAL 899 SECOND; ";
var reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
MailMessage mail = new MailMessage("Hone#PlaceHolder.com", "David#PlaceHolder.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mailrelay.PlaceHolder.com";
mail.Subject = "GIS Monitor for MSA 30 Min Alert.";
mail.Body = $" : {reader.GetValue(0)}";
try
{
client.Send(mail);
success = true;
}
catch (Exception ex)
{
lastException = ex;
}
}
}
}
return success;
}
}
}

ORA-64219: invalid LOB locator encountered

We migrated from oracle c12 to c19. And now we have simple test case that fails:
//arrange
string data = new string('x', 5000);
var connection = tm.GetConnection();
var createTableCmd = connection.CreateCommand(false);
createTableCmd.Text = "CREATE TABLE xx_temp (id NUMBER, text_long NCLOB, text_short NVARCHAR2(2000))";
createTableCmd.ExecuteNonQuery();
//act
var insertCmd = connection.CreateCommand(false);
insertCmd.Text = "INSERT INTO XX_TEMP (text_long) VALUES (#p1)";
var param = new OracleParameter("#p1", OracleDbType.NClob, data.Length, System.Data.ParameterDirection.Input);
param.Value = data;
insertCmd.Parameters.Add(param);
var insertResult = insertCmd.ExecuteNonQuery();
The last line fails with the error: ORA-64219: invalid LOB locator encountered
We're using Devart.Data.Oracle component. And I have no idea what is wrong.
What is interesting is that when I'm trying to connect to DB from powershell using Oracle.ManagedDataAccess.dll, I have no problems using similar code.
Please upgrade to dotConnect for Oracle v9.11.980. The following code works with v9.11.980 and Oracle 19c:
//arrange
string data = new string('x', 5000);
var connection = new OracleConnection();
connection.ConnectionString = #"User Id=****;Password=****;Server=YOUR_HOST/YOUR_SID;Direct=true;";
connection.Open();
var createTableCmd = connection.CreateCommand();
//createTableCmd.CommandText = "DROP TABLE xx_temp";
//createTableCmd.ExecuteNonQuery();
createTableCmd.CommandText = "CREATE TABLE xx_temp (id NUMBER, text_long NCLOB, text_short NVARCHAR2(2000))";
createTableCmd.ExecuteNonQuery();
//act
var insertCmd = connection.CreateCommand();
insertCmd.CommandText = "INSERT INTO XX_TEMP (text_long) VALUES (:p1)";
var param = new OracleParameter("p1", OracleDbType.NClob, data.Length, System.Data.ParameterDirection.Input);
param.Value = data;
insertCmd.Parameters.Add(param);
var insertResult = insertCmd.ExecuteNonQuery();

Connect to oracle database without tns and with OS authentication

Currenlty I am using the following connection string to connect to oracle database
string Source = new OracleConnectionStringBuilder()
{
DataSource = #"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = YOURHOST)(PORT = 1521)))(CONNECT_DATA =(SID = TESTORACLE)))",
}.ConnectionString;
private IDbConnection databasecon= new OracleConnection(Source);
I have no idea how to specify that connect using os authentication
Finally found the way to create Non TNS windows authentication connection stringstring Source = new OracleConnectionStringBuilder()
{
DataSource = #"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = YOURHOST)(PORT = 1521)))(CONNECT_DATA =(SID = TESTORACLE)))",
UserID = #"/",
}.ConnectionString;
private IDbConnection databasecon= new OracleConnection(Source);
without user id and password just use UserID = #"/" for windows authentication

Parameterized update query is not working in c#

I am trying to update details but the query is not working. client id is auto-generated.
here is my code
try
{
con.Open();
SqlCommand updatecmd = new SqlCommand();
updatecmd.CommandType = CommandType.Text;
// long client_id = CreateID();
updatecmd.CommandText= "UPDATE client_info SET companyname = #companyname, url = #url, industry = #industry, contactperson1 = #contactperson1, contactperson2 = #contactperson2, designation = #designation, fax = #fax, phone = #phone, mobile = #mobile, emailid1 = #emailid1, emailid2 = #emailid2, baddress = #baddress, bcity = #bcity, bstate = #bstate, bzipcode = #bzipcode, bcountry = #bcountry, regaddress = #regaddress, regcity = #regcity, regstate = #regstate, regzipcode = #regzipcode, regcountry = #regcountry WHERE client_id = #client_id";
// updatecmd.Parameters.AddWithValue("#client_id", client_id);
updatecmd.Parameters.AddWithValue("#companyname", txtcompanyname.Text);
updatecmd.Parameters.AddWithValue("#url", txturl.Text);
updatecmd.Parameters.AddWithValue("#industry", drpindustry.Text);
updatecmd.Parameters.AddWithValue("#contactperson1", txtcontactperson1.Text);
updatecmd.Parameters.AddWithValue("#contactperson2", txtcontactperson2.Text);
updatecmd.Parameters.AddWithValue("#designation", txtdesignation.Text);
updatecmd.Parameters.AddWithValue("#fax", txtfaxnumber.Text);
updatecmd.Parameters.AddWithValue("#phone", txtphone.Text);
updatecmd.Parameters.AddWithValue("#mobile", txtmobile.Text);
updatecmd.Parameters.AddWithValue("#emailid1", txtemailid1.Text);
updatecmd.Parameters.AddWithValue("#emailid2", txtemailid2.Text);
updatecmd.Parameters.AddWithValue("#baddress", txtbaddress.InnerText);
updatecmd.Parameters.AddWithValue("#bcity", txtbcity.Text);
updatecmd.Parameters.AddWithValue("#bstate", txtbstate.Text);
updatecmd.Parameters.AddWithValue("#bzipcode", txtbzipcode.Text);
updatecmd.Parameters.AddWithValue("#bcountry", bddlCountries.Text);
updatecmd.Parameters.AddWithValue("#regaddress",txtraddress.InnerText);
updatecmd.Parameters.AddWithValue("#regcity", txtrcity.Text);
updatecmd.Parameters.AddWithValue("#regstate", txtrstate.Text);
updatecmd.Parameters.AddWithValue("#regzipcode",txtrzipcode.Text);
updatecmd.Parameters.AddWithValue("#regcountry",
rddlCountries.Text);
updatecmd.ExecuteNonQuery();
Its not showing error and exception but data is not stored in DB.

Connect to Oracle without tnsname.ora

I need to connect to oracle from my .Net application.
I'm thinking of using ODP.NET
Is there a way to connect to Oracle without any dependency on the tnsnames.ora file? Reason I ask is because I'll have hundreds of different connections and I wouldnt want to be dependant on that file.
Yes, if you use a connection string that contains the data of tnsname.ora.
Say your tnsname entry looks like this:
YourTnsName =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = YourHost)(PORT = 1521))
)
(CONNECT_DATA =
(SID = YourSid)
)
)
instead of using YourTnsName in the connection string, you can write it like this:
var constr = new OracleConnectionStringBuilder()
{
DataSource = #"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = YourHost)(PORT = 1521)))(CONNECT_DATA =(SID = YourSid)))",
UserID = "userid",
Password = "password",
}.ConnectionString;
using (var con = new OracleConnection(constr))
{
...
}
hence no entry in tnsname.ora is needed.

Categories