【问题标题】:C# connection with MySQL DatabaseC# 与 MySQL 数据库的连接
【发布时间】:2020-05-27 08:45:29
【问题描述】:

再一次,我只是使用谷歌并通过论坛搜索并没有进一步了解,所以我们开始吧:

我正在尝试与 C# 和 MySQL 数据库(在 phpmyadmin 上运行)建立连接。

这是我目前得到的代码:

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


namespace csharpdatabasefinal
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("C#: Connect to MySQL Database using localhost." + Environment.NewLine);
            string connectionString = "server=localhost;user=csharptest;database=csharptest;port=3306;password=123";
            MySqlConnection conn = new MySqlConnection(connectionString);

            try
            {
                conn.Open();
                Console.WriteLine("Connection is " + conn.State.ToString() + Environment.NewLine);

                conn.Close();
                Console.WriteLine("Connection is " + conn.State.ToString() + Environment.NewLine);

            }catch(MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Error: " + ex.Message.ToString());
            }

            Console.WriteLine("Press any key to exit..");
            Console.Read();
        }
    }
}

这是我得到的错误,我猜它与参考有关,但我不太确定.. 我添加为参考“MySql.Data.dll”和“System.Security.Permissions.dll”。 我添加了 Systen.Security.Permission.dll,因为在之前的错误中他要求使用它。

这是我得到的错误:

Click me!(Error-Image)

这里是完整的错误代码:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.
  Source=MySql.Data
  StackTrace:
   at MySql.Data.MySqlClient.Replication.ReplicationManager.IsReplicationGroup(String groupName)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at csharpdatabasefinal.Program.Main(String[] args) in C:\Users\anonym\source\repos\csharpdatabasefinal\csharpdatabasefinal\Program.cs:line 19

Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

【问题讨论】:

  • 不用担心密码不同,我把图片和密码都改了,虽然只有本地的 ;)
  • 请在您的问题中包含完整的异常和堆栈跟踪作为文本。提示:单击屏幕截图中该对话框中的“复制详细信息”。
  • 我无法阅读屏幕截图中的所有错误消息,但它看起来像是一个文件未找到异常。您能否提供完整的错误消息并确定它说找不到哪个文件?
  • 我刚刚添加了完整的错误消息,我还添加了“System.Configuration” dll,但如果这就是您的建议,它就无法正常工作.. :/
  • 您是否尝试过在项目 -> 属性 -> 调试中启用“本机代码调试”?如果没有尝试通过 nuget 添加 MySQL dll:Install-Package MySql.Data -Version 8.0.20

标签: c# mysql reference connection


【解决方案1】:

在项目 -> 属性 -> 调试中启用“原生代码调试”

如果这不起作用,请尝试通过 nuget 添加 MySQL dll:

Install-Package MySql.Data -Version 8.0.20

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多