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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ByteFX.Data.MySqlClient;
namespace TestMySQL
{
/// <summary>
/// Description résumée de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tbhote;
private System.Windows.Forms.TextBox tblogin;
private System.Windows.Forms.Button btconnect;
private System.Windows.Forms.TextBox tbbase;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.TextBox tbpwd;
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
private System.ComponentModel.Container components = null;
private MySqlConnection Connection = new MySqlConnection();
private MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
private System.Windows.Forms.TextBox tableBox;
private DataSet ds = new DataSet();
public Form1()
{
//
// Requis pour la prise en charge du Concepteur Windows Forms
//
InitializeComponent();
//
// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
//
}
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Code généré par le Concepteur Windows Form
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InitializeComponent()
{
this.tbhote = new System.Windows.Forms.TextBox();
this.tblogin = new System.Windows.Forms.TextBox();
this.tbpwd = new System.Windows.Forms.TextBox();
this.btconnect = new System.Windows.Forms.Button();
this.tbbase = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.tableBox = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// tbhote
//
this.tbhote.Location = new System.Drawing.Point(24, 16);
this.tbhote.Name = "tbhote";
this.tbhote.TabIndex = 0;
this.tbhote.Text = "localhost";
//
// tblogin
//
this.tblogin.Location = new System.Drawing.Point(256, 16);
this.tblogin.Name = "tblogin";
this.tblogin.TabIndex = 1;
this.tblogin.Text = "login";
//
// tbpwd
//
this.tbpwd.Location = new System.Drawing.Point(376, 16);
this.tbpwd.Name = "tbpwd";
this.tbpwd.PasswordChar = '*';
this.tbpwd.TabIndex = 2;
this.tbpwd.Text = "password";
//
// btconnect
//
this.btconnect.Location = new System.Drawing.Point(656, 16);
this.btconnect.Name = "btconnect";
this.btconnect.TabIndex = 3;
this.btconnect.Text = "Connexion";
this.btconnect.Click += new System.EventHandler(this.btconnect_Click);
//
// tbbase
//
this.tbbase.Location = new System.Drawing.Point(144, 16);
this.tbbase.Name = "tbbase";
this.tbbase.TabIndex = 4;
this.tbbase.Text = "base";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 64);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(712, 256);
this.dataGrid1.TabIndex = 5;
//
// tableBox
//
this.tableBox.Location = new System.Drawing.Point(488, 16);
this.tableBox.Name = "tableBox";
this.tableBox.Size = new System.Drawing.Size(104, 20);
this.tableBox.TabIndex = 6;
this.tableBox.Text = "table";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(760, 342);
this.Controls.Add(this.tableBox);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.tbbase);
this.Controls.Add(this.btconnect);
this.Controls.Add(this.tbpwd);
this.Controls.Add(this.tblogin);
this.Controls.Add(this.tbhote);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "MySQL et .NET";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Point d'entrée principal de l'application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btconnect_Click(object sender, System.EventArgs e)
{
string ConnnectionStr = String.Empty;
if ( (tbhote.Text.Length == 0) || (tbbase.Text.Length == 0) || (tblogin.Text.Length == 0) )
{
MessageBox.Show("Erreur, les champs Hôte, Base et Login doivent être remplis", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
ConnnectionStr = "Server =mysql4.freehostia.com;Port=3306;Database=abdcho20;Uid=abdcho20;Pwd=******";}
Connection.ConnectionString = ConnnectionStr;
try
{
// On ouvre la connexion
Connection.Open();
// On définit la requête SELECT à exécuter
string MySQLCmd = "SELECT * FROM " + tableBox.Text; //phpbb_users
// On associe cette requête à la propriété SelectCommand du MySqlDataAdapter
MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd, Connection);
// On remplit le dataset avec le résultat de la commande SELECT
MyAdapter.Fill(ds);
// On affiche le datagrid
dataGrid1.DataSource = ds.Tables[0];
dataGrid1.Refresh();
// On ferme la connexion le plus tôt possible
Connection.Close();
}
catch(MySqlException Ex)
{
MessageBox.Show("Erreur SQL:\n" + Ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
} |
Partager