Using LINQ with MySql in ASP.NET 4.5 - c#

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.

Related

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/

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

Could not connect to SQL Server but Excel and ODBC config can

I have a C# program to connect to SQL Server. It works fine at the test computer with SQL Server 2012 but does not work on the production environment with 2008. At the production environment, it reports exception as such,
Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or in
stance-specific error occurred while establishing a connection to SQL Server. Th
e server was not found or was not accessible. Verify that the instance name is c
orrect and that SQL Server is configured to allow remote connections. (provider:
Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) --
-> System.ComponentModel.Win32Exception: Access is denied
here is my connection code,
connStrSql = "Server=" + sqlserver + "; Database=" + sqldb + "; Trusted_Connection=True";
SqlConnection sqlConn = new SqlConnection(connStrSql)
sqlConn.Open();
The target platform is x86 and target framework is 4.5. The funny thing is that Excel and ODBC config can connect to the database without complain. Does C# program use different way connecting to SQL Server? How can I fix the problem?
There are many possible reasons why you can not connect to an SQL Server database. This is a great trouble shooting guide to help you solve the above error.
Briefly:
Is the SQL Server services running?
Has the SQL Server TCP/IP settings been configured?
Does the firewall settings allow SQL Server through?
Has SQL Server itself been configured allow remote connections?
Other things to consider
Is the value of sqlserver correct?
Has the client protocol you are using to connection to SQL Server, such as Named Pipes, been enabled?
One of the most common reasons for a remote connection being refused in newer versions of SQL Server is the SQL Browser is not turned on. It is off by default after an install. Another common reason is you don't have the particular protocol (in this case net pipes) turned on, but I would check the SQL Browser first, as it is probably turned off.
Might be firewall/security restriction in production - Try running against a local SQL2008 db because I just ran the code against SQL2008 and it does work with a correct sqlserver and sqldb parameter and change the line below ( missing ; )
SqlConnection sqlConn = new SqlConnection(connStrSql);
Try this for SQL Server connection:
Attach a database file on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;
Try this Excel Connection string
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
If any other problem regarding connection string, refer this site
MS-Excel: http://www.connectionstrings.com/excel/
MS SQL Server: http://www.connectionstrings.com/sql-server/
Thanks,
NB

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