Code simple permettant de des transactions avec une base mysql.
La fonction query et getRes permettent de récupérer un très grand nombre de résulat (la limite est l'espace disque disponible)
La dll ajoutée est le connecteur avec la base.
public class cMySql
{
private int imax = 1000000;
private MySqlConnection pMySQL;
private string sHost = "";
private int iPort = 0;
private string sUser = "";
private string sPassword = "";
private string sDatabase = "";
private string sConnexionstring = "";
private string Erreur = "";
private bool bIsOk = false;
private int iResultInFic;
private string[] lastResult;
//--constructeurs. Se connecte au moment de l'initialisation
public cMySql()
: this("127.0.0.1", 22, "root", "", "")
{
}
public cMySql(string tsHost, int tiPort, string tsUser, string tsPassword, string tsDatabase)
{
sHost = tsHost;
iPort = tiPort;
sUser = tsUser;
sPassword = tsPassword;
sDatabase = tsDatabase;
sConnexionstring = "server=" + sHost + ";User Id=" + sUser + ";password=" + sPassword + ";database=" + sDatabase + ";Allow User Variables=true";
connect();
}
private void connect()
{
pMySQL = new MySqlConnection(sConnexionstring);
pMySQL.Open();
}
//--recuperer la derniere erreur
public string GetErreur() { return Erreur; }
//--lancer une requete de type query. renvoi {0:pas de resultats,1:ok,2:erreur}
public int Query(string sQuery)
{
int it = 0;
int ires = 0 ;
foreach(string s in Directory.GetFiles("c:\\","*.dump"))
File.Delete(s);
iResultInFic = 0;
Erreur = "";
lastResult = new string[0];
bIsOk = false;
int retour = 0;
MySqlCommand cmdMySQL = pMySQL.CreateCommand();
cmdMySQL.CommandText = sQuery;
MySqlDataReader reader;
try
{
reader = cmdMySQL.ExecuteReader();
}
catch (MySqlException e)
{
Erreur = e.Message.ToString();
return 2;
}
bIsOk = true;
int nbChamps = reader.FieldCount - 1;
string res = "";
List<String> tab = new List<String>();
while (reader.Read())
{
for (int i = 0; i <= nbChamps; i++)
res += reader.GetValue(i) + ";";
tab.Add(res);
it++;
if (it == imax)
{
iResultInFic++;
using (StreamWriter sw = new StreamWriter(@"c:\tmpSql_" + iResultInFic.ToString() + ".dump"))
foreach (string s in tab)
sw.WriteLine(s);
tab.Clear();
tab.Add(res);
it = 1;
}
ires++;
res = "";
}
if (tab.Count > 0)
retour = 1;
lastResult = new string[ires];
int j = 0;
foreach (string s in tab)
{
lastResult[j] = s;
j++;
}
reader.Close();
return retour;
}
public IEnumerable<string> getRes()
{
if (!bIsOk)
{
#region gestion
if (Erreur != "")
{
Exception e = new Exception(Erreur);
throw e;
}
else
{
Exception e = new Exception("PAS DE REQUETE");
throw e;
}
#endregion
}
if (iResultInFic > 0)
{
for (int i = 1; i <= iResultInFic; i++)
{
using (StreamReader sr = new StreamReader(@"c:\tmpSql_" + iResultInFic.ToString() + ".dump"))
{
while (sr.Peek() >= 0)
yield return sr.ReadLine();
}
}
}
foreach (string s in lastResult)
yield return s;
}
//--Executer une requete insert, 0 en cas d'erreur, 1 si ok
public int Exec(string sExec)
{
MySqlCommand cmdMySQL = pMySQL.CreateCommand();
Erreur = "";
cmdMySQL.CommandText = sExec;
try
{
cmdMySQL.ExecuteNonQuery();
}
catch (MySqlException e)
{ Erreur = e.Message.ToString(); return 0; }
return 1;
}
//--récupérer edernier identifiant insérer
public int lastInsertId()
{
MySqlCommand cmdMySQL = pMySQL.CreateCommand();
cmdMySQL.CommandText = "SELECT LAST_INSERT_ID()";
MySqlDataReader reader = cmdMySQL.ExecuteReader();
try
{
if (reader.Read())
{
return reader.GetInt32(0);
}
else
{
return 0;
}
}
finally
{
reader.Close();
}
}
//--modifier base
public void set_base(String tsBase)
{
sDatabase = tsBase;
sConnexionstring = "server=" + sHost + ";User Id=" + sUser + ";password=" + sPassword + ";database=" + sDatabase + ";Allow User Variables=true";
connect();
}
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.