Editeur de batch pour lots de requetes sql pour mysql

Contenu du snippet

Voila, c'est un truc tout con, qui sert a rien, mais je l'ai fait... En fait, il faut saisir une série de requetes SQL conformes (car mon appli verifie pas la syntaxe !) et en cliquant sur creer, l'appli génère un fichier contenant toutes les requetes et un exécutable qui lancera MySQL et exécutera les requetes ! L'avantage est que ca permet aussi l'execution de requete d'administration (privilege, droit, creation de bd, etc...).
A quoi ca peut etre utile ? Bah, il arrive que vous ayez a faire des lots de requetes assez souvent, une fois par semaine, ou plus, notamment des requetes d'administration. Grace a ce batch et au planificateur de taches de Win XP, vous pourrez faire en sorte que ce soit automatique et ne plus vous faire chier. Bref, ca va pas servir a grand monde...

En revanche, c'est ma premiere appli en C# (qui fonctionne du moins) donc j'attends avec impatience vos commentaire, conseils et critiques qui me permettront de m'ameliorer dans le langage !

A plus et bonne lecture !

Source / Exemple :


using System;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace DefaultNamespace
{
	/// <summary>
	/// Description of MainForm.	
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtSQL;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button cmdReset;
		private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtUser;
		private System.Windows.Forms.Button cmdCreer;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.cmdCreer = new System.Windows.Forms.Button();
			this.txtUser = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
			this.cmdReset = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.txtSQL = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// cmdCreer
			// 
			this.cmdCreer.Location = new System.Drawing.Point(408, 240);
			this.cmdCreer.Name = "cmdCreer";
			this.cmdCreer.TabIndex = 7;
			this.cmdCreer.Text = "&Créer";
			this.cmdCreer.Click += new System.EventHandler(this.CmdCreerClick);
			// 
			// txtUser
			// 
			this.txtUser.Location = new System.Drawing.Point(104, 8);
			this.txtUser.Name = "txtUser";
			this.txtUser.Size = new System.Drawing.Size(160, 20);
			this.txtUser.TabIndex = 3;
			this.txtUser.Text = "";
			this.txtUser.WordWrap = false;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 8);
			this.label3.Name = "label3";
			this.label3.TabIndex = 2;
			this.label3.Text = "Utilisateur :";
			// 
			// cmdReset
			// 
			this.cmdReset.Location = new System.Drawing.Point(328, 240);
			this.cmdReset.Name = "cmdReset";
			this.cmdReset.TabIndex = 8;
			this.cmdReset.Text = "&Reset";
			this.cmdReset.Click += new System.EventHandler(this.CmdResetClick);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.TabIndex = 6;
			this.label2.Text = "Requêtes :";
			// 
			// txtSQL
			// 
			this.txtSQL.Location = new System.Drawing.Point(8, 56);
			this.txtSQL.Multiline = true;
			this.txtSQL.Name = "txtSQL";
			this.txtSQL.Size = new System.Drawing.Size(472, 176);
			this.txtSQL.TabIndex = 5;
			this.txtSQL.Text = "";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(488, 270);
			this.Controls.Add(this.cmdReset);
			this.Controls.Add(this.cmdCreer);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtSQL);
			this.Controls.Add(this.txtUser);
			this.Controls.Add(this.label3);
			this.Location = new System.Drawing.Point(-21, 0);
			this.Name = "MainForm";
			this.Text = "MainForm";
			this.Load += new System.EventHandler(this.MainFormLoad);
			this.ResumeLayout(false);
		}
		#endregion
		
		
		void MainFormLoad(object sender, System.EventArgs e)
		{
			this.folderBrowserDialog.Description = "Sélectionnez le répertoire où vous désirez enregistrer les fichiers.\n(Conseil : mettez les dans un répertoire à eux pour les retrouver plus facilement.)";
		}
		
		void CmdCreerClick(object sender, System.EventArgs e)
		{
			string sFichierBatch;
			string sFichierSQL;
			string sCommande;
			
				
			if((this.txtUser.Text != "") && (this.txtSQL.Text!=""))
			{
				if((this.folderBrowserDialog.ShowDialog() == DialogResult.OK)&&((MessageBox.Show("Si des fichiers ont déjà été créés à cet emplacement, ils seront perdus.\nEtes-vous sûr de vouloir sélectionner ce dossier ?","Attention",MessageBoxButtons.YesNo, MessageBoxIcon.Hand)) == DialogResult.Yes))
				{
					string sChemin = this.folderBrowserDialog.SelectedPath ;
				
					if(sChemin[sChemin.Length-1].ToString() != "\\")
						sChemin += "\\";
				
					
					sFichierBatch = sChemin + "Executable.bat";
					sFichierSQL = sChemin + "Requete.sql";
					
					StreamWriter fBatch = File.CreateText(sFichierBatch);
					StreamWriter fSQL = File.CreateText(sFichierSQL);
					
					sCommande = "mysql -u " + this.txtUser.Text + " -p < Requete.sql";
					fBatch.WriteLine(sCommande);
					fBatch.Close();
					fSQL.Write(this.txtSQL.Text);
					fSQL.Close();
					if(MessageBox.Show("Création des fichiers terminée.\nVoulez vous exécuter les requêtes maintenant ?","Création terminée",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
					{
						Process pBatch = new Process();
						pBatch.StartInfo = new ProcessStartInfo();
						pBatch.StartInfo.FileName = sFichierBatch;
						pBatch.Start();
					}
				}
			}
			else
				MessageBox.Show("Vous devez saisir un nom d'utilisateur (enregistré dans MySQL avec les droits nécessaires de préférence) et des requêtes à exécutées !","Erreur de saisie",MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		void CmdResetClick(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("Etes-vous sûr de vouloir effacer tous les champs ?","Confirmation",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				this.txtUser.Text = "";
				this.txtSQL.Text = "";
			}
		}
		
	}
}

Conclusion :


Allez, je me suis un peu laché sur les boites de dialogue, mais c'est pas grave, c'est marrant ! lol
Sinon, il y a surement des bugs, mais j'en ai pas vu. Signalez les moi pour que je corrige ca !

(Ils sont marrants, il demande le niveau ??? Bah, c'est du code expert, ca, au moins, non ????)

A voir également

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.