c# - 在 ASP.NET 4.5 中将 LINQ 与 MySql 结合使用

标签 c# mysql linq

当我尝试使用 LINQ 的 DataContext 和连接字符串连接到 MySql 数据库时,由于无法找到网络路径,它在运行时失败。当我连接到 MS SQL 数据库时,这曾经有效。它在运行时失败,但有以下异常:

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

失败代码:

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();

当我直接使用 MySqlConnection 时,它可以工作,所以我知道我的连接字符串和服务器配置是好的:

MySqlConnection conn = new MySqlConnection(connectionString);
conn.Open();
conn.Close();
return "Test successful.";

连接字符串如下所示:

Database={database_name};Data Source={webhost.net};User Id={mysql_user};Password={password}

在我的 .csproj 中,我使用以下引用作为 MySql Connector 6.9.9 installer 的一部分:

MySql.Data
MySql.Data.Entity.EF6
MySql.Web

我怀疑 LINQ 不知道我正在尝试连接到 MySql 数据库,并且正在尝试像 MS SQL 数据库一样使用连接字符串。有没有办法将 LINQ 的 DataContext 与 MySql 数据库一起使用?

最佳答案

您正在连接 MySql 数据库,并从“System.Data.SqlClient.SqlException”生成错误,这意味着它无法连接 MySql 数据库并尝试连接 Sql 数据库。

尝试在服务器资源管理器中通过DataConnection添加MySql数据库enter image description here

如果您没有获得此选项,那么您必须安装“Visual studio MySql Connector” 安装成功后,重新启动 VS。

关于c# - 在 ASP.NET 4.5 中将 LINQ 与 MySql 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43198800/

相关文章:

c# - 从数据库 Linq 获取最小值和最大值到 MVC 中的 Sql 查询?

c# - 使用 NHibernate QueryOver 按间隔分组

c# - 需要单次点击数据库

c# - <a> 标签刷新了我的 asp.net 页面

c# - 如何在 C# 中使用 selenium webdriver 单击 javascript 确认对话框

php - 在 Laravel 应用程序中,mysql import 将时间戳 null 值转换为 0000-00-00 00 :00:00

mysql - 检查 field = '' 与 field IS NULL 是否有任何速度差异?

c# - 您如何使用 MVP 将服务层消息/错误传达给更高层?

php - 如何将秒表结果(数组)推送到第一个表结果(数组)

c# - Entity Framework 中的大规模增删多对多关系