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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;
using System.Text.RegularExpressions;
namespace Projets_Machine_2011
{
public partial class Mod_machines : Form
{
//=================================================================================================================================================================
DataSet ds = new DataSet(); // creation d'un data set permettant de s'agit d'un composant très générique permettant de représenter les données sous forme de table.
// ================================================================================================================================================================
SqlConnection conn = new SqlConnection("Data source=T129-P7; Initial Catalog=gestion_projet_2011; Integrated Security = True"); // Creation d'une connexion a la base de donnéés
//==========================================================================================//
// conn.Open(); //
// MessageBox.Show(conn.State.ToString()); //
// conn.Close(); //
//==========================================================================================//
//===================================================================================================================================================================================================================================================================
SqlDataAdapter commande = new SqlDataAdapter(); // Représente un ensemble de commandes de données et une connexion de base de données qui sont utilisés pour remplir DataSet et mettre à jour une base de données SQL Server. Cette classe ne peut pas être héritée.
//===================================================================================================================================================================================================================================================================
BindingSource BS = new BindingSource();
public Mod_machines()
{
InitializeComponent();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox7_Click(object sender, EventArgs e)
{
this.Refresh();
}
private void btn_mdf_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("Vous devez remplir les champs Ip , Nom , Num et Emplacement pour valider le formulaire", "Saisie incomplète", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
// Test du champ de l'adressn Ip =================================================================
string message = "";
string ip = textBox1.Text;
if (!Regex.IsMatch(ip, "^\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}[.]\\d{1,3}$")
||
ip.Split(new char[] { '.' })
.Select(s => int.Parse(s))
.Where(i => i > 255 || i == 0)
.Count() > 0
)
{
message += "\n- Format de l'ip incorrect.";
}
//============================================================================================================
// Test du champ numero ===========================================================================
string num = textBox2.Text;
if (!Regex.IsMatch(num, "^\\d{1,100000}$"))
{
message += "\n- Format du num est incorrect.";
}
//============================================================================================================
// Test du champ Nom =====================================================================================
int nom;
if (int.TryParse(textBox3.Text, out nom))
{
message += "\n- Format du Nom incorrect.";
}
//=============================================================================================================
// Test du champ Emplacement ================================================================================
int Emplacement;
if (int.TryParse(textBox4.Text, out Emplacement))
{
message += "\n- Format de l'emplacement incorrect.";
}
if (message.Length > 0)
{
MessageBox.Show("Erreur :" + message);
}
//================================================================================================== ===========
else
{
int i;
commande.UpdateCommand = new SqlCommand("Update machines SET IP = @IP , NUM = @NUM , NOM = @NOM , EMPLACEMENT = @EMPLACEMENT ", conn);
commande.UpdateCommand.Parameters.Add("@IP", SqlDbType.VarChar).Value = textBox1.Text;
commande.UpdateCommand.Parameters.Add("@NUM", SqlDbType.VarChar).Value = textBox2.Text;
commande.UpdateCommand.Parameters.Add("@NOM", SqlDbType.VarChar).Value = textBox3.Text;
commande.UpdateCommand.Parameters.Add("@EMPLACEMENT", SqlDbType.VarChar).Value = textBox4.Text;
// commande.UpdateCommand.Parameters.Add("@IP", SqlDbType.Int).Value = ds.Tables[0].Rows[BS.Position][0];
conn.Open();
i = commande.UpdateCommand.ExecuteNonQuery();
conn.Close();
if (i >= 1)
MessageBox.Show(" Votre enregistrement(s) a été bien Mis à Jour");
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
commande.SelectCommand = new SqlCommand("select * from Machines", conn);
ds.Clear();
commande.Fill(ds);
dataGrid.DataSource = ds.Tables[0];
BS.DataSource = ds.Tables[0];
textBox1.DataBindings.Add(new Binding("text", BS, "IP"));
textBox2.DataBindings.Add(new Binding("text", BS, "Num"));
textBox3.DataBindings.Add(new Binding("text", BS, "Nom"));
textBox4.DataBindings.Add(new Binding("text", BS, "Emplacement"));
enregistrement();
}
private void DatagridUpdate()
{
dataGrid.ClearSelection();
dataGrid.Rows[BS.Position].Selected = true;
enregistrement();
}
private void enregistrement()
{
Enregisrtrement.Text = " Enregistrement " + BS.Position + " sur " + (BS.Count - 1);
}
private void dataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void pictureBox5_Click(object sender, EventArgs e)
{
BS.MoveFirst();
enregistrement();
DatagridUpdate();
}
private void pictureBox4_Click(object sender, EventArgs e)
{
BS.MovePrevious();
enregistrement();
DatagridUpdate();
}
private void pictureBox6_Click(object sender, EventArgs e)
{
BS.MoveLast();
enregistrement();
DatagridUpdate();
}
private void pictureBox3_Click(object sender, EventArgs e)
{
BS.MoveNext();
enregistrement();
DatagridUpdate();
}
}
} |
Partager