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
Related
Installed Plesk on my VS. After this I can't setup SSH-tunnel to get data from other server-database. Using SSH.NET with below code. (This code works locally). Configured to allow communication for port 3306 in Plesk firewall. Any suggestions on how to solve this issue?
Error:
[SocketException (0x271d): An attempt was made to access a socket in a
way forbidden by its access permissions]
Code:
DataTable dt = new DataTable();
string IP = "ssh.xxxxxxxx.xxx";
string Username = "xxxxxxxxxx";
string password = "xxxxxxxxxx";
var connInfo = new Renci.SshNet.PasswordConnectionInfo(IP, Username, password);
using (var sshClient = new Renci.SshNet.SshClient(connInfo))
{
sshClient.Connect();
if (sshClient.IsConnected)
{
Renci.SshNet.ForwardedPortLocal port =
new Renci.SshNet.ForwardedPortLocal("127.0.0.1", 3306, "xxxxxxxx", 3306);
sshClient.AddForwardedPort(port);
port.Start();
using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;PORT=3306;UID=xxxxxx;PASSWORD=xxxxxxx;DATABASE=xxxxxxx; convert zero datetime=True"))
{
string tmpsql = "Select fname,lname,username FROM tbluser Where id=#id";
using (MySqlCommand cmd = new MySqlCommand(tmpsql, con))
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("id", MySqlDbType.Int16).Value = UserID;
da.Fill(dt);
}
}
}
sshClient.Disconnect();
}
The local port which you are trying to forward is most probably already used by another application (like a local MySQL database server).
Use another port. Or even better, let the system pick any free local port:
var port = new ForwardedPortLocal("127.0.0.1", "dbserver.example.com", 3306);
client.AddForwardedPort(port);
port.Start();
var connectionString =
$"SERVER={port.BoundHost};PORT={port.BoundPort};" +
"UID=xxxxxx;PASSWORD=xxxxxxx;DATABASE=xxxxxxx; convert zero datetime=True";
using (MySqlConnection con = new MySqlConnection(connectionString))
{
// ...
}
Related: C# SSH tunnel Postgres database connection
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
I've tried to create remote MySql connection via ssh tunnel forwardedport.
The sshClient connection OK.
ForwardedPort starts OK.
When I try to connect with MysqlConnection it throws System.Net.IO.IOException with the message "The handshake failed due to an unexpected packet format"
The port is OK 100% sure because other native app(eg HeidiSQL) can connect if i create this port with my app.
PrivateKeyFile file = new PrivateKeyFile(rsaFile);
client = new SshClient(host, port, username, file);
forwardBoundHost = "127.0.0.1";
forwardBoundPort = 33306;
forwardHost = "127.0.0.1";
forwardPort = 3306;
port = new ForwardedPortLocal(forwardBoundHost, forwardBoundPort, forwardHost, forwardPort);
if(this.response != null){
port.RequestReceived += response;
}
client.Connect();
client.AddForwardedPort(port);
port.Exception += port_Exception;
port.Start();
if (port.IsStarted)
{
cb = new MySqlConnectionStringBuilder()
{
AllowBatch = true,
Server = this.host,
Port = this.port,
UserID = this.dbuser,
Password = this.dbpassword,
Database = this.database,
SslMode = MySqlSslMode.Required,
Keepalive = 60,
ConnectionProtocol = MySqlConnectionProtocol.Tcp,
CharacterSet = "utf8"
};
cb.ConnectionProtocol = MySqlConnectionProtocol.Tcp;
MySqlConnection connection = new MySqlConnection(cb.GetConnectionString(true));
MySqlCommand cmd;
MySqlDataReader reader;
try
{
Console.WriteLine("Mysql client conn");
connection.Open();
}
cmd = connection.CreateCommand();
cmd.CommandText = queryString;
cmd.Prepare();
Array myp = new Array[param.Length];
int i = 0;
foreach (String oneParam in param)
{
myp.SetValue(new MySqlParameter(oneParam, MySqlDbType.String), i);
i++;
}
cmd.Parameters.AddRange(myp);
reader = cmd.ExecuteReader();
}
catch (Exception e)
{
//Logger(e.ToString());
throw (e);
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
connection.Close();
}
return reader;
I found a solution for my problem. Because of using the Forwarded Port, the default port 3306 changed to 33306 in connection string, so (and tell me if i'm wrong) the MySQLClient changed the SslMode from None (in my first attempts it was not set) to any of Required or Preffered etc...
Tried to set it to MySqlSslMode.None and it worked like charm :) finally!
Using SSH.Net and MySqlClient
Connect to server with SSHClient(ConnectionInfo)
Start ForwardedPortLocal(127.0.0.1, 33306, 127.0.0.1, 3306)
Connect MySql WITHOUT ANY SSLMode (MySqlSSLMode.None)
Here is the code!
cb = new MySqlConnectionStringBuilder()
{
AllowBatch = true,
Server = this.host,
Port = this.port,
UserID = this.dbuser,
Password = this.dbpassword,
Database = this.database,
SslMode = MySqlSslMode.None,
Keepalive = 60,
ConnectionProtocol = MySqlConnectionProtocol.Tcp,
CharacterSet = "utf8"
};
cb.ConnectionProtocol = MySqlConnectionProtocol.Tcp;
MySqlConnection connection = new MySqlConnection(cb.GetConnectionString(true));
MySqlCommand cmd;
MySqlDataReader reader;
try
{
Console.WriteLine("Mysql client conn");
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = queryString;
cmd.Prepare(); ....
If you need any help let me know.
I have multiple SQL databases with the same schema .Say(Database1,Database2....)
How do i dynamically select a database in Entity framework model in runtime?.Since they have the same schema, it does not make sense to import all the data models before hand.
You can change database connection string like this:
DataModelContainer context = new DataModelContainer(
ConnectionOperation.CreateEntityConnection());
And this is CreateEntityConnection Method:
internal static EntityConnection CreateEntityConnection()
{
// Start out by creating the SQL Server connection string
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
// Set the properties for the data source. The IP address network address
sqlBuilder.DataSource = System.Configuration.ConfigurationManager.AppSettings["Connection"];
// The name of the database on the server
sqlBuilder.UserID = "sa";
sqlBuilder.Password = "12345";
sqlBuilder.InitialCatalog = "DatabaseName";
sqlBuilder.IntegratedSecurity = true;
sqlBuilder.MultipleActiveResultSets = true;
// Now create the Entity Framework connection string
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = "System.Data.SqlClient";
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
// Set the Metadata location.
entityBuilder.Metadata = #"res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl";
// Create and entity connection
EntityConnection conn = new EntityConnection(entityBuilder.ToString());
return conn;
}
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.