如何在 Visual Studio 中连接到 MySQL 数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4235291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:45:36  来源:igfitidea点击:

How to connect to a MySQL Data Source in Visual Studio

mysqlvisual-studio-2010datasourcemysql-connector

提问by JohnB

I use the MySQL Connector/Netto connect to my database by referencing the assembly (MySql.Data.dll) and passing in a connection string to MySqlConnection. I like that because I don't have to install anything.

我使用MySQL Connector/Net通过引用程序集 (MySql.Data.dll) 并将连接字符串传递到MySqlConnection. 我喜欢这样,因为我不需要安装任何东西。

Is there some way to "Choose Data Source" in Visual Studio 2010 without installing something?

有没有什么方法可以在 Visual Studio 2010 中“选择数据源”而不安装一些东西?

How can I get a MySQL option (localhost) to show up on one of these lists? Or do I have to install something?

如何让 MySQL 选项 (localhost) 显示在这些列表之一中?还是我必须安装一些东西?

(I don't want to use ODBC btw)

(顺便说一句,我不想​​使用 ODBC)

"Add Connection" from Server Explorer:alt text

服务器资源管理器中的“添加连接”:替代文字

Entity Data Model Wizard:alt text

实体数据模型向导:替代文字

采纳答案by Lucas

Visual Studio requires that DDEX Providers (Data Designer Extensibility) be registered by adding certain entries in the Windows Registry during installation (HKLM\SOFTWARE\Microsoft\VisualStudio\{version}\DataProviders) . See DDEX Provider Registrationin MSDN for more details.

Visual Studio 要求通过在安装期间在 Windows 注册表中添加某些条目来注册 DDEX 提供程序(数据设计器扩展性) ( HKLM\SOFTWARE\Microsoft\VisualStudio\{version}\DataProviders)。有关更多详细信息,请参阅MSDN 中的DDEX 提供程序注册

回答by Jon Black

install the MySQL .NET Connector found here http://dev.mysql.com/downloads/connector/net/

安装在这里找到的 MySQL .NET 连接器http://dev.mysql.com/downloads/connector/net/

alt text

替代文字

回答by Memet Olsen

"Starting with version 6.7, Connector/Net will no longer include the MySQL for Visual Studio integration. That functionality is now available in a separate product called MySQL for Visual Studio available using the MySQL Installer for Windows."

“从 6.7 版开始,Connector/Net 将不再包含用于 Visual Studio 集成的 MySQL。该功能现在可在名为 MySQL for Visual Studio 的单独产品中使用,该产品使用适用于 Windows 的 MySQL 安装程序可用。”

Source: http://dev.mysql.com/downloads/connector/net/6.6.html

来源:http: //dev.mysql.com/downloads/connector/net/6.6.html

回答by Sulyman

After a lot of searching and trying many solutions, I got it finally:

经过大量搜索和尝试多种解决方案,我终于得到了它:

  1. uninstall connector

  2. uninstall MySQL for Visual Studio from control panel

    click here

  3. reinstall them according to the table below

    click here

  4. copy the assembly files from C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.8\Assemblies\v4.5to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE

  5. log off and reopen your solution

  6. enjoy

  1. 卸载连接器

  2. 从控制面板卸载 MySQL for Visual Studio

    点击这里

  3. 根据下表重新安装它们

    点击这里

  4. 将程序集文件复制C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.8\Assemblies\v4.5C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE

  5. 注销并重新打开您的解决方案

  6. 请享用

回答by stuzor

This seems to be a common problem. I had to uninstall the latest Connector/NET driver (6.7.4) and install an older version (6.6.5) for it to work. Others report 6.6.6 working for them.

这似乎是一个普遍的问题。我必须卸载最新的 Connector/NET 驱动程序 (6.7.4) 并安装旧版本 (6.6.5) 才能使其工作。其他人报告 6.6.6 为他们工作。

See other topic with more info: MySQL Data Source not appearing in Visual Studio

有关更多信息,请参阅其他主题:MySQL 数据源未出现在 Visual Studio 中

回答by Ognyan Dimitrov

  1. Download MySQL Connector .NET (6.9.4 on this date) from hereand install it CUSTOM!
  2. Remove the ASP.NET WEB providers option or the installer will write in machine.config!
  3. Download MySQL for Visual Studio from hereand install it CUSTOM. Be sure to check the integration options. You need this step because after Connector .NET 6.7 the installer will no longer integrate the connector with Visual Studio. This installer can take longer then expected. This is it.
  1. 这里下载 MySQL Connector .NET(此日期为 6.9.4)并安装它CUSTOM
  2. 删除 ASP.NET WEB 提供程序选项,否则安装程序将写入 machine.config!
  3. 这里下载 MySQL for Visual Studio并安装它CUSTOM。请务必检查集成选项。您需要执行此步骤,因为在 Connector .NET 6.7 之后,安装程序将不再将连接器与 Visual Studio 集成。此安装程序可能需要比预期更长的时间。就是这个。

You can install it from alternate download herewhich should have integrated with VS correctly but it did not and I got a strange error and after the reinstall it is ok.

您可以从此处的备用下载安装它,它应该已与 VS 正确集成,但它没有,我遇到了一个奇怪的错误,重新安装后就可以了。

回答by j.xavier.atero

Installing the following packages:

安装以下软件包:

adds MySQL Databaseto the data sources list (Visual Studio 2017)

MySQL 数据库添加到数据源列表 (Visual Studio 2017)

回答by Onjon Shahadat

View ImageI have got the same problem for my vs 2013 on 64-bit machine. So i tried to download MySql extension for VSand install it on my machine. and restart the vs.

查看图片我的 vs 2013 在 64 位机器上遇到了同样的问题。因此,我尝试为 VS下载MySql 扩展并将其安装在我的机器上。并重新启动 vs。

回答by Jordan

In order to get the MySQL Databaseitem in the Choose Data Sourcewindow, one should install the MySQL for Visual Studiopackage available here (the last version today is 1.2.6):

为了在“选择数据源”窗口中获取MySQL 数据库项,应安装此处提供的MySQL for Visual Studio包(今天的最后一个版本是1.2.6):

https://dev.mysql.com/downloads/windows/visualstudio/

https://dev.mysql.com/downloads/windows/visualstudio/

回答by Jeff D

Right Click the Project in Solution Explorer and click Manage NuGet Packages

右键单击解决方案资源管理器中的项目,然后单击管理 NuGet 包

Search for MySql.Data package, when you find it click on Install

搜索 MySql.Data 包,找到后点击安装

Here is the sample controller which connects to MySql database using the mysql package. We mainly make use of MySqlConnection connection object.

这是使用 mysql 包连接到 MySql 数据库的示例控制器。我们主要利用 MySqlConnection 连接对象。

 public class HomeController : Controller
{
    public ActionResult Index()
    {
        List<employeemodel> employees = new List<employeemodel>();
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            string query = "SELECT EmployeeId, Name, Country FROM Employees";
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
                cmd.Connection = con;
               con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        employees.Add(new EmployeeModel
                        {
                            EmployeeId = Convert.ToInt32(sdr["EmployeeId"]),
                            Name = sdr["Name"].ToString(),
                            Country = sdr["Country"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }

        return View(employees);
    }
}