C# 使用 MySql.Data.MySqlClient; 不管用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1422456/
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-06 16:22:50  来源:igfitidea点击:

using MySql.Data.MySqlClient; is not working

c#mysql

提问by

Before I used MS SQL but in a new project I use mysql and when I run our application I get this error

在我使用 MS SQL 之前,但在一个新项目中,我使用了 mysql,当我运行我们的应用程序时,我收到此错误

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?)

编译错误说明:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS0246:找不到类型或命名空间名称“MySql”(您是否缺少 using 指令或程序集引用?)

Source Error:

源错误:

Line 4:  using System.Text;
Line 5:  using System.Web;
Line 6:  `using MySql.Data.MySqlClient;    this namespace is not working 
Line 7:  using System.Data.SqlTypes;

How can I solve this problem?

我怎么解决这个问题?

回答by Lukasz Lysik

Are you sure you have connector libraries?

你确定你有连接器库吗?

回答by Thomas Levesque

You need to add a reference to the MySQL.Data.dll (if you don't have it, you can download it by following the link posted by Lukasz)

您需要添加对 MySQL.Data.dll 的引用(如果您没有,可以按照 Lukasz 发布的链接下载)

回答by Fathah Rehman P

To solve this problem download MySql.Data.dll from

要解决此问题,请从以下位置下载 MySql.Data.dll

https://www.cryptool.org/trac/CrypTool2/export/2020/trunk/AppReferences/x64/MySql.Data.dll

https://www.cryptool.org/trac/CrypTool2/export/2020/trunk/AppReferences/x64/MySql.Data.dll

Then right click on your project name, click on add reference and select MySql.Data.dll file

然后右键单击您的项目名称,单击添加引用并选择 MySql.Data.dll 文件

回答by yagmurdursun

In Project solution frame find Referencesfile and right click on it, then choose "Add Reference" on the menu, then click on .NET tab, find MySql.Dataand add it to References file.

在 Project 解决方案框架中找到References文件并右键单击它,然后在菜单上选择“添加引用”,然后单击 .NET 选项卡,找到MySql.Data并将其添加到引用文件中。

回答by Pierre Sundberg

1) Right-click on References, select Add Reference.

1) 右键单击​​引用,选择添加引用。

2) Select .NET tab. Check what the text directly under the tabs says, example: "Filtered to: .NET Framework 4 Client Profile".

2) 选择 .NET 选项卡。检查选项卡正下方的文本内容,例如:“过滤为:.NET Framework 4 客户端配置文件”。

3) Click on the Browse tab.

3) 单击浏览选项卡。

4) Browse to the MySql folder and go on into the Assemblies folder, example: C:\Program Files (x86)\MySQL\Connector NET 6.8.3\Assemblies

4) 浏览到 MySql 文件夹并进入 Assemblies 文件夹,例如:C:\Program Files (x86)\MySQL\Connector NET 6.8.3\Assemblies

5) Select the version folder which matches the text under 2), example: "v4.0"

5)选择与2)下文本匹配的版本文件夹,例如:“v4.0”

6) Add MySql.Data.dll

6) 添加MySql.Data.dll

回答by Kuky

On Windows 7 64bit

在 Windows 7 64 位上

1) download https://www.cryptool.org/trac/CrypTool2/export/2020/trunk/AppReferences/x64/MySql.Data.dll

1)下载https://www.cryptool.org/trac/CrypTool2/export/2020/trunk/AppReferences/x64/MySql.Data.dll

2) paste a copy of MySql.Data.dll in folder /bin

2) 将 MySql.Data.dll 的副本粘贴到文件夹 /bin 中

3) create the database and table e.g.

3)创建数据库和表例如

CREATE DATABASE my_db ;

USE my_db; 

CREATE TABLE `my_users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`country` varchar(100) NOT NULL default '',
PRIMARY KEY  (`id`));

INSERT INTO `my_users` VALUES (null,'Mark','Canada');

INSERT INTO `my_users` VALUES (null,'Frank','US');

4) create the following page Default.aspx

4) 创建如下页面 Default.aspx

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="MySql.Data.MySqlClient" %>
<script runat="server">

protected void Page_Load(Object sender, EventArgs e)
{
    MySqlConnection myConnection = new MySqlConnection(
    "server=localhost; user id=root; password=zZxX11++; database=my_db; pooling=false;");

    String strSQL = "SELECT * FROM my_users;";

    MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(strSQL, myConnection);

    DataSet myDataSet = new DataSet();
    myDataAdapter.Fill(myDataSet, "my_users");

    MySQLDataGrid.DataSource = myDataSet;
    MySQLDataGrid.DataBind();
}

</script>
<html>
<head>
    <title>MySQL Query</title>
</head>
<body>
    <form runat="server">
    <asp:DataGrid id="MySQLDataGrid" runat="server"></asp:DataGrid>
    </form>
</body>
</html> 

5) download http://www.codeguru.com/dbfiles/get_file/060216.zip?id=19637&lbl=060216_ZIP

5)下载http://www.codeguru.com/dbfiles/get_file/060216.zip?id=19637&lbl=060216_ZIP

6) extract the above and place GridView.aspxin your project folder

6) 提取以上内容并将GridView.aspx放在您的项目文件夹中

7) run the Default.aspx

7) 运行Default.aspx

8) The output should be:

8)输出应该是:

Table content:

表内容:

<table cellspacing="0" rules="all" border="1" id="MySQLDataGrid" style="border-collapse:collapse;">
    <tbody><tr>
        <td>id</td><td>name</td><td>country</td>
    </tr><tr>
        <td>1</td><td>Mark</td><td>Canada</td>
    </tr><tr>
        <td>2</td><td>Frank</td><td>US</td>
    </tr>
</tbody></table>

回答by Hao Shi

You should download MySQL.Data.dll.

您应该下载MySQL.Data.dll

Because MySql is not in .Net framework library, so if you just click "Add Reference" in your project on VS, you cannot find it.

因为MySql不在.Net框架库中,所以如果在VS上的项目中直接点击“添加引用”,是找不到的。

After you download it, you can click Add Reference to put it into your project.

下载后,您可以单击添加引用将其放入您的项目中。

回答by y4cO

If you use MySqlyou need to add to the references MySql.Data.MySqlClientand use Mysqlconnection, that worked for me.

如果您使用MySql,您需要添加到引用MySql.Data.MySqlClient并使用Mysqlconnection,这对我有用

回答by vivek

If you are using Unity Development IDE:

如果您使用的是Unity 开发 IDE

1) Right-click on References, select Edit Reference. enter image description here

1) 右键单击​​引用,选择编辑引用。 在此处输入图片说明

2) Make Sure that System.Data(in All Tab) and MySQl.Data is checked(in .NET Assembly package).

2)确保 System.Data(在所有选项卡中)和 MySQl.Data 被检查(在 .NET Assembly 包中)。

Note:If MySQl.Data is not there, download it from http://ul.to/g2o3a3h1and then upload it to Unity Environment using Assets->Import Package->Custom Package.

注意:如果 MySQl.Data 不存在,请从http://ul.to/g2o3a3h1下载,然后使用 Assets->Import Package->Custom Package 将其上传到 Unity Environment。

3) Reopen your database handler file and CLEAN and REBUILD your code. If still reference error persist, do the step 1 and 2

3) 重新打开您的数据库处理程序文件并清理并重新构建您的代码。如果仍然引用错误,请执行步骤 1 和 2

回答by Brackets

You might not need to download the dll, you might already have it. View->Object Browser->.NET->find MySql.Data->Add->OK-> Add to Referencesenter image description here

您可能不需要下载 dll,您可能已经有了它。 查看->对象浏览器->.NET->find MySql.Data-> Add-> OK-> Add to References在此处输入图片说明