当我尝试使用 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数据库
如果您没有获得此选项,那么您必须安装“Visual studio MySql Connector” 安装成功后,重新启动 VS。
关于c# - 在 ASP.NET 4.5 中将 LINQ 与 MySql 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43198800/