Teoh Che Leong
Teoh Che Leong

Reputation: 21

'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception

below is my connection. Can anyone help look on this please. Got the error when tried to launch the windows form application.

The error is:

System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;

namespace Csharp_Simple_Hotel_System
{
    /*
     *  in this class will make the connection between our app and mysql database
     *  first you need to download the mysql connector and add it to your project
     *  download link - >
     */
    class CONNECT
    {
        private MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=csharp_hotel_db");

        // create a function to return our connection
        public MySqlConnection getConnection()
        {
            return connection;
        }

        //create a function to open the connection
        public void openConnection()
        {
            if(connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        //create a function to close the connection
        public void closeConnection()
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }
    }
}

Let me know if you need further information for me.

Thank you.

Upvotes: 0

Views: 1093

Answers (1)

Teoh Che Leong
Teoh Che Leong

Reputation: 21

Found a solutions. Instead of install mysql connector windows package, we can install mysql data from nuGET package from Microsoft Visual Studio.

Can go to Tools > nuGET Package Manager > manage nuGet Package for solutions > browse for Mysql connector and install it.

BOOM! Problem SOLVED!

Upvotes: 2

Related Questions