0

我试图为我的项目连接到我的 mysql 数据库,但我收到以下错误:

错误 CS0246:找不到类型或命名空间名称“MySql”

完整错误:

[运行] mono "C:\Users\Aidan\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "d:!computer science!!NEA!\test stuff\sql\c# sql test 1 .cs”错误:无法编译指定的文件。

csscript.CompilerException: d:!computer science!!NEA!\test stuff\sql\c# sql test 1.cs(3,7): error CS0246: The type or namespace name 'MySql' could not be found(你错过了吗? using 指令或程序集引用?) d:!computer science!!NEA!\test stuff\sql\c# sql test 1.cs(4,7): error CS0246: The type or namespace name 'MySql' could not be找到(您是否缺少 using 指令或程序集引用?) d:!computer science!!NEA!\test stuff\sql\c# sql test 1.cs(5,7): error CS0246: The type or namespace name '找不到 MySql'(您是否缺少 using 指令或程序集引用?)

在 csscript.CSExecutor.ProcessCompilingResult(System.CodeDom.Compiler.CompilerResults 结果,System.CodeDom.Compiler.CompilerParameters compilerParams,CSScriptLibrary.ScriptParser 解析器,System.String scriptFileName,System.String assemblyFileName,System.String[] additionalDependencies)[0x00102]在 :0 在 csscript.CSExecutor.Compile (System.String scriptFileName) [0x0080d] 在 :0 在 csscript.CSExecutor.ExecuteImpl () [0x005a1] 在 :0

[完成] 在 5.388 秒内以 code=1 退出

我正在使用 Visual Studio 代码,(Visual Studio 坏了,说社区的免费试用期已过)

当在网上查看类似这样的其他问题时,我找不到任何可以解决的问题,一些网站正在讨论 dll 和其他东西,但我无法理解他们想要做什么,所以请准确解释我需要做什么做。

我使用 c#,在同一台计算机上运行服务器,我能够连接到它并使用 popsql 编辑数据库。

这是我正在使用的代码:

using System;
using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

public class Tutorial2
{
    public static void Main()
    {
        string connStr = "server=localhost;user=****;database=*****;port=****;password=***********";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();

            string sql = "SELECT Name, HeadOfState FROM Country WHERE Continent='Oceania'";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]+" -- "+rdr[1]);
            }
            rdr.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        conn.Close();
        Console.WriteLine("Done.");
    }
}

(我用星星替换了密码等)

4

2 回答 2

0

您可以尝试以下方法:

  1. 检查项目参考。如果引用中存在 MySql 但有警告(黄色三角形),则需要删除并再次添加 .dll。更简单的方法是安装 nuget 包。

  2. 或者,检查项目的框架。如果项目的.Net版本小于MySql的版本,则需要更改项目的目标框架。

于 2019-02-21T00:23:12.753 回答
0

如果您在使用标签时遇到错误,很可能不是您的代码错误,而是您要引用的文件。某些 MySQL dll 可能已损坏或丢失。

于 2019-02-20T23:57:40.950 回答