mysql - MVC2 和 mySQL 数据库

标签 mysql linq-to-sql asp.net-mvc-2

我遵循 ASP.NET MVC 2 Framework 书籍(Steven Sanderson)中的示例,但我使用的是 mySQL,而不是 SQLServer。

public class ProductsController : Controller
{
    private IProductsRepository productsRepository;
    public ProductsController()
    {
        // Temporary hard-coded connection string until we set up dependency injection
        string connString = "server=xx.xx.xxx.xxx;Database=db_SportsStore;User Id=dbUser;Pwd=xxxxxxxxx;Persist Security Info=True;";
        productsRepository = new SportsStore.Domain.Concrete.SqlProductsRepository(connString);

    }
    public ViewResult List()
    {
        return View(productsRepository.Products.ToList());
    }
}

当我运行该项目时,出现以下错误:

与 SQL Server 建立连接时发生网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)

我已经下载了 MySQL .Net 连接器,并且在服务器资源管理器中我能够查看该数据库上的表。我还确保服务器上的数据库允许远程连接。

我通过 VS2010 在本地创建并运行代码,MySQL 数据库位于托管服务器上。

我需要做什么才能让这个 MVC2 示例适用于 MySQL?

更新:

我在等待答案的同时修改了代码,并将 ProductsController 更新为以下内容:

using MySql.Data.MySqlClient;
            string connString = "server=xx.xx.xxx.xxx;Database=db_SportsStore;User Id=dbUser;Pwd=xxxxxx;";
            var connection = new MySqlConnection(connString);

            productsRepository = new SportsStore.Domain.Concrete.SqlProductsRepository(connection);

然后在存储库中:

public SqlProductsRepository(MySql.Data.MySqlClient.MySqlConnection connection)
{
    connection.Open();
    productsTable = (new DataContext(connection)).GetTable<Product>();
    connection.Close();
}

现在我得到了一个完全不同的错误:

{“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解在 '[t0].[ProductID], [t0] 附近使用的正确语法。 [名称]、[t0].[描述]、[t0].[价格]、[t0].[类别]'位于第 1 行”}

我还应该提到...此示例使用 LINQ to SQL。我不确定这是否重要。请指教。

最佳答案

我想到了一些想法。

  1. 如果您要连接到 SQL Server 数据库而不是 MySQL .Net Connector 连接字符串,则您使用的连接字符串就是连接字符串。您必须将书中的连接字符串示例更改为 MySQL 需要的内容。例如,MySQL 没有“可信连接”选项。 (http://dev.mysql.com/doc/refman/5.1/en/connector-net-connection-options.html)
  2. 在 SQLProductsRepository 中,您需要使用 MySQLConnection 对象等,而不是 SQLConnection 对象(如果使用)。我认为这本书使用了 Linq To SQL 或 Linq to Entity Framework。无论哪种方式,您都必须修改代码才能使用 MySQL...对象或 modify the model to hit your MySQL如果是 Entity Framework 。如果您已经这样做了,那么其中的连接字符串将很好地了解您在 #1 中需要什么。

http://dev.mysql.com/doc/refman/5.1/en/connector-net-tutorials-intro.html

编辑 - 随着您的更新...

MySQL 有时与 SQL Server 的语法不同。我假设这就是这个错误所指向的。

在 MySQL 连接器连接字符串上,它有 an option to use SQL Server Syntax称为 SQL Server 模式。将其设置为 true 并查看是否有效......

类似于:

Server=serverIP;Database=dbName;Uid=Username;Pwd=Password;SQL Server Mode=true;

关于mysql - MVC2 和 mySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5843376/

相关文章:

mysql - 在 MySQL 数据库上使用版本控制 (Git)

linq-to-sql - LINQ to SQL 序列包含多个元素

asp.net-mvc-2 - 如何仅使用 DataAnnotations 验证 Viewmodel 中的部分字段?

c# - 为什么 Html.Checkbox ("Visible") 在 ASP.NET MVC 2 中返回 "true, false"?

mysql - 只获取表中存在的行?

php - 安全地实现 "configurable"加入系统

c# - 无法连接另一台服务器上的Mysql

c# - Linq中的 "SELECT SYSTEM_USER"相当于什么

linq - 学习 LinqToSql 还是坚持使用 ADO.NET?

asp.net-mvc-2 - DTO 在 ASP.NET MVC 中的使用