Unable to connect to MySql Server in Visual C# - 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/

Related

Using LINQ with MySql in ASP.NET 4.5

When I try to connect to the MySql database using LINQ's DataContext and a connection string, it fails at runtime due to not being able to find the network path. This used to work when I was connecting to a MS SQL database. It fails at runtime with the exception:
System.Data.SqlClient.SqlException: '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)'
Inner Exception
Win32Exception: The network path was not found
Failing code:
DataContext db = new DataContext(connectionString);
Table<FlightPriceModel> flightsTable = db.GetTable<FlightPriceModel>();
IQueryable<FlightPriceModel> query = from row in flightsTable select row;
return "Number of rows in FlightPrices table: " + query.Count();
When I use MySqlConnection directly, it works, so I know my connection string and server configuration are good:
MySqlConnection conn = new MySqlConnection(connectionString);
conn.Open();
conn.Close();
return "Test successful.";
The connection string looks like this:
Database={database_name};Data Source={webhost.net};User Id={mysql_user};Password={password}
In my .csproj I'm using the following references as part of the MySql Connector 6.9.9 installer:
MySql.Data
MySql.Data.Entity.EF6
MySql.Web
I suspect that LINQ doesn't know I'm trying to connect to a MySql database and is trying to use the connection string as it were for a MS SQL database. Is there a way to use LINQ's DataContext with a MySql database?
You are connecting MySql database and error genrate from "System.Data.SqlClient.SqlException"that means it could not connect MySql detabase and try to connect with Sql database.
Try to add MySql database by DataConnection in Server Explorer
If u will not get this option then you have to install "Visual studio MySql Connector"
After successfull install restart your VS.

provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server, pgsql

I am using Dapper to connect to pgsql database. The below code was working few days back. But don't know why its not working now. The same credentials are working with pgadmin3 but not with the c# code.
here is the controller method, for simplicity I wrote all db code in Get method.
the below code was
public IEnumerable<Item> Get()
{
string connString = "Server=192.168.1.11;Database=db_alpha1;Uid=postgres;Pwd=xxxxxx";
using (IDbConnection db = new SqlConnection(connString))
{
db.Open();
return db.Query<Item>("Select * From items").ToList();
}
}
Here is the error on line db.Open();:
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)
Also, I already configured pg_hba.conf file, added my system ip address.
Thanks to #Steve. The issue has resolved, it was a silly mistake, I replaced
SqlConnection with NpgsqlConnection, and added npgsql package.

Connection to SQL Server Express from Visual Studio

I created a database using SQL Server Express from Visual Studio 2012 and I fail to open a connection pointing that database.
I've tried many way like for example:
SqlConnection connectionLocale = new SqlConnection(#"Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\Users\Max\Desktop\presentoir\Application\WpfAppTest\BDD.mdf;Integrated Security=True");
But I keep on getting the same error message :
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) (Microsoft SQL Server, Error: 1326)
As I am a total beginner, even with two days of research, I really can't figure out what the problem is.
I can get a connection using :
SqlConnection connectionLocale = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|BDD.mdf;Integrated Security=True");
But I can't change my database using C# code by connecting like this (I think it creates a temporary database in appdata, and that all the modifications are done in this database).
Every idea helping me to create a connection to my database is welcome.
Okay, a coworker found the solution.
God, it was obvious...
With the # behind the string, the correct data source was :
#"Data Source=(LocalDB)\v11.0
and not :
#"Data Source=(LocalDB)\\v11.0

Connection string for SQL Server?

I am working with SQL Server. I used all connection via :
SqlConnection con = new SqlConnection("data source = .; database = xyzDatabase; integrated security = true");//developed with developer edition.
Now I want to run my application on SQL Server Express, so I used this :
SqlConnection con = new SqlConnection("data source = .\\sqlexpress; initial catalog = xyzDatabase; user id = sa; password = 123");
Point to remember is, I am creating my xyzDatabase at runtime on form load event. But when I execute the program I get this error :
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)
(.Net SqlClient Data Provider)
Import the namespaces
using System.Data.SqlClient (if using C#)
or
Imports System.Data.SqlClient (if using VB#)
Your Sql connection object with connection would be like this:
SqlConnection con = new SqlConnection("Data Source=.;Database=xyzDatabase;User Id=sa;Password=admin#123;");
If still your error not resolved, it might be the problem of incorrect installation of MS-SQL SERVER instances.
http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/
Hope it might be helpful.
you must active sa user in sql express edition.
SqlConnection cn = new SqlConnection("datasource=.\sqlexpress; initialcatalog=yzDatabase; userid=sa;password=123");

Connecting to a database

I'm trying to connect to a database but nothing I try works.
SqlConnection conn = new SqlConnection(#"Data Source=C:\Users\Gerard Foley\Desktop\Northwind.sdf");
conn.Open();
No matter what I try I just get the error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I stole the connection string from Database Explorer -> Properties -> Connection String. What am I missing? I can get the tables to show up in a DataGridView fine (by dragging from Data Sources), but I want to use my own UI and queries. I just can't seem to figure this ADO thing out.
Using c# express 2010 and sql server express 2008.
for the proper connection string to use to connect to SQL Server have a look at:
http://connectionstrings.com
the connection string you are using now is strange, it should contain server name and database name, see link above for examples...
GOT IT. I should have been using Sql*Ce*Connection. The connection string was fine.
You need to specify 'AttachDbFileName' in the connection string. See the examples for sql server express here: http://www.connectionstrings.com/sql-server-2008.

Categories