-2

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}");
        }
    }
}
  1. On the command prompt:
>csc program.cs /r:MySql.Data.dll
>program
MySQL version : 8.4.7
  1. 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?

4
  • 2
    "I added a link to the file MySql.Data.dll to the Dependencies branch" wrong place to do it. You should use NuGet or other package manager to load the package and all its dependencies nuget.org/packages/mysql.data Commented Nov 15 at 18:33
  • to @Charlieface: In this case, why does it compile from the command line without errors? Commented Nov 15 at 18:43
  • 2
    Possibly csc is loading the library from the GAC. Also csc.exe compiles to .NET Framework which has access to different libraries. Commented Nov 15 at 18:44
  • It could be that the DLL is found at compile time, but not when running the program. Using it through a NuGet package should prevent that from happening. Commented Nov 15 at 18:45

1 Answer 1

4

By default, Visual Studio Community 2022 with an appropriate workload has two templates for creating console C# applications for Windows:

  1. Console App
  2. Console App (.NET Framework)

The first one is for projects on the .NET.

If you select the first one, the Dependencies branch will appear in the Solution Explorer. If you select the second one, there will be a References branch in the Solution Explorer. Both options are described in the documentation.

In the first case, for working with MySQL, as I understand it, the best option is to install the 'MySql.Data' package using the NuGet package manager built into Visual Studio. It works for me.

In the second case, you can just add a reference to the MySql.Data.dll file received from here. It also works for me.

As I understand it (thanks to @Charlieface for his comments), my csc.exe (Roslyn) from 'Developer Command Prompt for VS 2022' creates an executable file for the .NET Framework, not for .NET. As I understand it, MySql.Data.dll for .NET Framework and MySql.Data.dll for .NET are two files that differ from each other.

Correct me if I made a mistake somewhere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.