1 pièce(s) jointe(s)
Problème de connexion à mysql en c#
Bonjour à tous,
Je débute en c# et j'essaie de créer une connexion depuis un programme sur mon laptop à ma base de données Mysql qui ce trouve sur mon ordinateur de bureau.
J'ai installé : mysql-connector-net-8.0.21 téléchargé sur le site officiel: https://dev.mysql.com/downloads/connector/net/
J'ai aussi installé: mysql-for-visualstudio-1.2.9 téléchargé sur le site officiel: https://dev.mysql.com/downloads/windows/visualstudio/
J'utilise visual studio 2019 avec la dernière mise a jour disponible.
À première vue, le code me semble bon. Cependant, le programme bloc au niveau de: connexion.Open(); J'ai une erreur, la voici:
System.TypeInitializationException*: 'The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception.'
Exception interne
FileNotFoundException*: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Le fichier spécifié est introuvable.
J'aimerais comprendre pourquoi ça marche pas afin d'apprendre et continuer aussi lol.
Le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//Librairie Mysql ajouté précédament dans les références mysql-connector-net-8.0.21
using MySql.Data.MySqlClient;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string host = "192.168.0.149";
//string host = "DESKTOP-BI80DAL"; //J'ai même testé cela à la place
int port = 3306;
string database = "tennis";
string username = "laptop";
string password = "root";
string comm = "Server=" + host + "; port=" + port + "; Database=" + database + "; User Id=" + username + "; password=" + password + ";";
MySqlConnection connexion = new MySqlConnection(comm);
try
{
connexion.Open(); // le programme bloc ici
MessageBox.Show("Connecte");
}
catch(MySqlException co)
{
MessageBox.Show("Non Connecte Erreur: "+ co.ToString());
}
}
}
} |
Pièce jointe 577578