简体   繁体   English

在ASP.NET 4.5中将LINQ与MySql一起使用

[英]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. 当我尝试使用LINQ的DataContext和连接字符串连接到MySql数据库时,由于无法找到网络路径,它在运行时失败。 This used to work when I was connecting to a MS SQL database. 当我连接到MS SQL数据库时,此方法可以正常工作。 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时,它可以工作,所以我知道我的连接字符串和服务器配置都很好:

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 : 在我的.csproj中,我将以下引用用作MySql Connector 6.9.9安装程序的一部分

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. 我怀疑LINQ不知道我要连接到MySql数据库,而是要像使用MS SQL数据库一样使用连接字符串。 Is there a way to use LINQ's DataContext with a MySql database? 有没有办法在MySql数据库中使用LINQ的DataContext?

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. 您正在连接MySql数据库,并从“ System.Data.SqlClient.SqlException”生成错误,这意味着它无法连接MySql数据库,并尝试与Sql数据库连接。

Try to add MySql database by DataConnection in Server Explorer 尝试在服务器资源管理器中通过DataConnection添加MySql数据库 在此处输入图片说明

If u will not get this option then you have to install "Visual studio MySql Connector" After successfull install restart your VS. 如果您无法获得此选项,则必须安装“ Visual Studio MySql Connector”。安装成功后,重新启动VS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM