与 MySQL 数据库的 C# 连接
C# connection with MySQL Database
再一次,我只是使用 google 并在论坛中搜索并没有取得任何进展,所以我们开始吧:
我正在尝试与 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.
在项目 -> 属性 -> 调试中启用 "native code debugging"
如果这不起作用,请尝试通过 nuget 添加 MySQL dll:
Install-Package MySql.Data -Version 8.0.20
再一次,我只是使用 google 并在论坛中搜索并没有取得任何进展,所以我们开始吧:
我正在尝试与 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.
在项目 -> 属性 -> 调试中启用 "native code debugging"
如果这不起作用,请尝试通过 nuget 添加 MySQL dll:
Install-Package MySql.Data -Version 8.0.20