I am a novice programmer. I want to work in my 'Windows 10' with MySQL Community Server 8.4 from a C# program (platform .NET 8.0). I am using the Visual Studio Community 2022. I also use MySQL Connector/NET 9.5.0.
I use two methods: 1) I write the code in a third-party code editor and compile it from Developer Command Prompt for VS 2022 with csc.exe, or 2) I create a project in Visual Studio Community 2022 and compile it from this environment. In the first case, everything works out, in the second case it doesn't.
My test C# program in program.cs:
using System;
using MySql.Data.MySqlClient;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello, World!");
string cs = "server=localhost;userid=s;password=1234567;database=ilyadb";
using var con = new MySqlConnection(cs);
con.Open();
Console.WriteLine($"MySQL version : {con.ServerVersion}");
}
}
}
- On the command prompt:
>csc program.cs /r:MySql.Data.dll
>program
MySQL version : 8.4.7
- In Visual Studio Community 2022:
I have created a console project using the platform .NET 8.0. In the program.cs file, I put the code identical to the above. I added a link to the file MySql.Data.dll to the Dependencies branch in Solution Explorer as shown in the documentation. However, after compiling, I got an error:
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Не удается найти указанный файл.
File name: 'System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at MySql.Data.MySqlClient.MySqlConnection.AssertPermissions()
at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Boolean execAsync, CancellationToken cancellationToken)
at MySql.Data.MySqlClient.MySqlConnection.Open()
at ConsoleApp1.Program.Main(String[] args) in D:\Академия ТОП\ПС, Технология доступа к БД ADO.NET\ConsoleApp1\Program.cs:line 13
My questions: 1) why does everything work in the first case, but in the second case it turns out to be an error? 2) how to get rid of the error in the second case?