C# Oracle Connectionstring - c#

I've got a little problem between oracle and C#. I can't seem to connect to my database(=localhost) within my application. I'm using Oracle XE11.2. The error occurs everytime I'm calling "conn.Open()" and Oracle returns this error: {"ORA-12154: TNS:could not resolve the connect identifier specified"}.
I'm 100% sure that the User Id and the Password are correct because I can login to the database via SQL Developer. I think the problem might be in the data source but I'm not sure. Anyone who can help out?
Here is my code:
string connstring = "Data Source=xe;User Id=Software;Password=Software";
//Open connection
OracleConnection conn = new OracleConnection(connstring);
conn.Open();

Did you try tns ping "tnsping xe" from the machine where you are running your program?

Related

C# connect to System ODBC datasource

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.

Unable to connect to MySql Server in Visual C#

I am trying to create a local database using MySql in Visual C# but connection is not getting established in the code but when i add the server in the server explorer under data connections its working. I tried test connection it says connection succeeded but its not working in the code.
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
con = new SqlConnection(connStr);
This throws an error saying
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
You cannot use the SqlConnection class to connect to a MySQL server; that is for Microsoft SQL Server only.
Instead, install the MySqlConnector NuGet package and use the following code:
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
using (var con = new MySqlConnection(connStr))
{
// ...
}
Your connection string is wrong.. change keys "user" to "Uid" and "password" to "Pwd".
follow eg:
string connStr = "server=localhost;Uid=root;database=entry_database;Pwd=superadmin";
con = new SqlConnection(connStr);
Correct, the format used by you to create the connection string is incorrect, giving you the error you mentioned above.
Actually, the way the connection string is written in MS-SQL is very different from MY-SQL. Every database has its own way of connecting and its individual connection tags:-
Please find the list of connection details for MS_SQL below. This would help you not only in the current mentioned scenario but also will provide a reference for future changes:-
https://www.connectionstrings.com/mysql/

DB connection working with old code and not with new

I am facing some issues with connectivity .
Question-
1)Is it possible that Web application access the databse but not console application?
2)When I am connecting my console app to our databse on server-"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible ",this error is coming though when I test the connection using setting tab in VS2010 for console application,it says Connection succeed,but as soon as i write Con.open,I get the error mentioned.
3)Also,If i am not able to connect to my db with new application,how come previous build web application on production are working fine.?
NOTE-There has been massive change in Server-Sql instances and all servers were upgraded.
Do I make sense?
Code I am using-
SqlConnection con;
SqlDataReader reader;
try
{
int id;
con = new SqlConnection("Data Source=DAN\\C1;Initial Catalog=tablenames;User ID=user;Password=123");
con.Open();//Error comes here
Exact same connection string works for webapplication but not for Console.Is it have something to do with sql server upgarde and changes.
Any help or guidance?
okay well from what i can see you may want to check that you have imported and used all the required namespaces involved in an SQL connection , you will need to import the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
After ensuring all the correct namespaces have been imported then you should check your connection string maybe there is a typing error or something there , remember if you make a mistake on the connection string the connection will be refused , and im not sure if this code will work but here goes try this method of connecting :
SqlConnection con;
SqlDataReader reader;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
con = new SqlConnection(connetionString);
try
{
int id;
con.Open();
Console.WriteLine("Connection Open ! ");
cnn.Close();
}
catch (Exception ex) { Console.WriteLine("Can not open connection ! "); }
If the above code gives you problems you can try this other method of doing a SQL connection in a C# console project as seen in the link below:
http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-create-single-connection-string-in-console-applicatio/
First check in the Server Explorer whether you are connected to the Server or database.If u connected to the server, server name will be visible under data connection, right click on it go to properties there u can find connection string value. copy paste that value in your sql connection code. Try this it may solve your connection string problem.
Thanks everyone.But it was some security issue.Once I deploy my code on server,its working.Remote connectivity was somehow restricted in the sql server.

Unable to connect to any of the specified MySQL hosts in c# windows form

I want to connect my windows form application to mysql .It works local mysql server perfectly.But it didn't work in online mysql.
My code is
string mysqlconnection = "Server=mysql.main-hosting.com;Database=xxxxxxxxx;Uid=yyyyyyy;Password=zzzzzzzz;";
MySqlConnection connection = new MySqlConnection(mysqlconnection);
connection.Open();
It shows Unable to connect to any of the specified MySQL hosts..How to solve this?
Try
Server=mysql.main-hosting.com;Database=xxxxxxxxx;User ID=yyyyyyy;Password=zzzzzzzz;
Instead of
Server=mysql.main-hosting.com;Database=xxxxxxxxx;Uid=yyyyyyy;Password=zzzzzzzz;
Maybe it is a connectionstring problem.
Or otherwise the server address is wrong.

Error connecting to Amazon EC2 MySQL from C#

I have an AWS EC2 instance running. I've tried to make a connection to my MySQL like this in C#
MySqlConnection l_DBConn = new MySqlConnection();
l_DBConn.ConnectionString = "Server=my-ec2.compute-1.amazonaws.com;Port=3306;Database=mydatabase;uid=root;password=mypassword;port=3306;charset=utf8"
MySqlCommand command = l_DBConn.CreateCommand();
command.CommandText = "select * from users";
MySqlDataReader Reader = command.ExecuteReader();
The error received when I run the last line is "Connection must be valid and open.". I still can connect to my instance using Filezilla,Putty and MySQL WorkBench also(with SSH). And I'm using MySql.Data.MySqlClient in my code. Anyone has any idea about this? Is this because my instance does not allow remote access or a problem with my connection string?
Thank you in advance,
Wayne.
Edit: The problem is solved by adding l_DBConn.Open() and add my IP to the server using RANT ALL PRIVILEGES ON . TO root#'hostname' IDENTIFIED BY 'root-password'. Thanks!
Try calling l_DBConn.open() before executing the reader. The error implies that the connection is not open since you haven't explicitely opened it
Some ideas:
Make sure you can connect to your database locally first.
Is Sql Server setup to allow mixed authentication
Have you open port 3306 in the EC2 firewall and the windows firewall if you're running it.

Categories