j'ai actuellement un script pour me connecter à ma bdd et pour agir sur mon champs points voir photo ci dessous seul probleme dans ma scene j'ai 2 boutons le premier sert a me rajouter 10 points et le second me sert a synchroniser mes points entre le jeu et ma bdd
le probleme cest que je le fais 1 fois tous marche j'ai bien la synchronisation qui marche mais si je veux le faire une 2e fois j'ai cette erreur que je n'arrive pas à corriger :
MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
voila mon code si une âme bien aimable aurai la gentillesse de bien vouloir m'aider ça serai extrêmement appréciable
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
using System;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using UnityEngine.SceneManagement;
public class sqlManager : MonoBehaviour
{
public static sqlManager instance;
[Header("DATABASE")]
public string host;
public string database, username, password;
[Header("REGISTER")]
public Canvas Canvasregister;
public InputField RPseudo;
public InputField RPassword, Rnom, Rprenom, REmail;
public Text Rtxtinfos;
MySqlConnection con;
[Header("Login")]
public Canvas canvaslogin;
public InputField Lpseudo;
public InputField Lpassword;
public Text Ltxtinfo;
[Header("info User")]
public string IPseudo;
public string INom, Iprenom, IEmail;
public int IPoints;
void Start ()
{
connectbdd();
}
void Awake ()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}
void connectbdd()
{
string cmd = "SERVER=" + host + ";" + "database =" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=true;Charset=utf8";
try
{
con = new MySqlConnection(cmd);
con.Open();
}
catch (Exception ex)
{
Debug.Log(ex.ToString());
}
}
void Update()
{
Debug.Log(con.State);
}
bool IsValidLenght(string InputString, int LenghtString)
{
if(InputString.Length>LenghtString)
{
return true;
}else
{
return false;
}
}
bool Isvalidemail(string InputEmail)
{
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(InputEmail);
if (match.Success)
{
return true;
}
else
{
return false;
}
}
bool Isvalid()
{
ColorBlock cbEmail = REmail.colors;
if (!Isvalidemail(REmail.text))
{
Rtxtinfos.text = "Invalid Email";
cbEmail.normalColor = Color.red;
REmail.colors = cbEmail;
return false;
}
else
{
Rtxtinfos.text = "";
cbEmail.normalColor = Color.white;
REmail.colors = cbEmail;
}
//Pseudo
ColorBlock cbPseudo = RPseudo.colors;
if(!IsValidLenght(RPseudo.text,4))
{
cbPseudo.normalColor = Color.red;
Rtxtinfos.text = "Pseudo invalid";
RPseudo.colors = cbPseudo;
return false;
}
else
{
cbPseudo.normalColor = Color.white;
Rtxtinfos.text = "";
RPseudo.colors = cbPseudo;
}
//password
ColorBlock cbPassword = RPassword.colors;
if (!IsValidLenght(RPassword.text, 4))
{
cbPassword.normalColor = Color.red;
Rtxtinfos.text = "Password invalid";
RPassword.colors = cbPassword;
return false;
}
else
{
cbPassword.normalColor = Color.white;
Rtxtinfos.text = "";
RPassword.colors = cbPseudo;
}
//other
if(!IsValidLenght(Rnom.text,0) || !IsValidLenght(Rprenom.text,0))
{
Rtxtinfos.text = "Empty not autorized";
return false;
}
//verification existance peseudo bdd
try
{
connectbdd();
MySqlCommand Cmdsql = new MySqlCommand("SELECT * FROM `utilisateurs` WHERE `pseudo`='" + RPseudo.text + "'",con);
MySqlDataReader MyReader = Cmdsql.ExecuteReader();
string data = null;
while(MyReader.Read())
{
data = MyReader["password"].ToString();
if(data !=null)
{
Rtxtinfos.text = "Pseudo Exist";
MyReader.Close();
return false;
}
}
MyReader.Close();
}
catch(Exception ex) { Debug.Log(ex.ToString()); }
Rtxtinfos.text = null;
return true;
}
public void Register ()
{
if(Isvalid())
{
string cmd= "INSERT INTO `game`.`utilisateurs` (`id`, `pseudo`, `password`, `nom`, `prenom`, `email`, `points`) VALUES(NULL, '"+RPseudo.text +"', '"+Md5Sum(RPassword.text)+"', '"+Rnom.text+"', '"+Rprenom.text+"', '"+REmail.text+"', '0')";
MySqlCommand cmdsql = new MySqlCommand(cmd, con);
try
{
cmdsql.ExecuteReader();
Rtxtinfos.text = "Register Succesfull";
}
catch (Exception ex)
{
Rtxtinfos.text = ex.ToString();
}
}
else
{
Debug.Log("non valid");
}
}
string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
public void Login()
{
try
{
connectbdd();
MySqlCommand Cmdsql = new MySqlCommand("SELECT * FROM `utilisateurs` WHERE `pseudo`='" + Lpseudo.text + "'", con);
MySqlDataReader MyReader = Cmdsql.ExecuteReader();
string data = null;
while (MyReader.Read())
{
data = MyReader["password"].ToString();
if (data == Md5Sum(Lpassword.text))
{
Ltxtinfo.text = "Login Succesfull";
//recuperation données utilisateurs
Iprenom = MyReader["prenom"].ToString();
INom= MyReader["nom"].ToString();
IEmail = MyReader["email"].ToString();
IPseudo = MyReader["pseudo"].ToString();
IPoints =(int)MyReader["points"];
SceneManager.LoadScene("menu");
}
else
{
Ltxtinfo.text = "Wrong Login or Password";
}
}
if(data==null)
{
Ltxtinfo.text = "Account not existing ! ";
}
MyReader.Close();
}
catch (Exception ex) { Debug.Log(ex.ToString()); }
}
public void ShowRegister ()
{
canvaslogin.gameObject.SetActive(false);
Canvasregister.gameObject.SetActive(true);
}
public void Showlogin()
{
canvaslogin.gameObject.SetActive(true);
Canvasregister.gameObject.SetActive(false);
}
public void SavePoints()
{
string cmd = "UPDATE `utilisateurs` SET `points`=" +IPoints+ " WHERE `pseudo`='" + IPseudo + "'";
MySqlCommand Cmdsql = new MySqlCommand(cmd, con);
try
{
Cmdsql.ExecuteReader();
Debug.Log("Update Succesfull");
}
catch (Exception Ex)
{
Debug.Log(Ex.ToString());
}
}
}
merciii encore
.