0% found this document useful (0 votes)
25 views8 pages

Connecteé + Deconnecter

Uploaded by

Manelle Bouyahyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Connecteé + Deconnecter

Uploaded by

Manelle Bouyahyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

CONNECTE

Program connecteé
public static string ch =
"server=(local);database=tarik;integrated security=true";
public static SqlConnection con = new SqlConnection(ch);
public static ArrayList Col = new ArrayList();
public static int pos;
premier
Program.pos=0;
Employe x = new Employe();
x = (Employe)Program.Col[Program.pos];
textBox1.Text=x.num.ToString();
textBox2.Text=x.Nom.ToString();

siuvent

if(Program.pos<Program.Col.Count-1)
{
Program.pos=Program.pos+1;
Employe x = new Employe();
x = (Employe)Program.Col[Program.pos];
textBox1.Text=x.num.ToString();
textBox2.Text=x.Nom.ToString();
}

Precedent

if(Program.pos>0)
{
Program.pos=Program.pos-1;
Employe x = new Employe();
x = (Employe)Program.Col[Program.pos];
textBox1.Text=x.num.ToString();
textBox2.Text=x.Nom.ToString();
}

Dernier

Program.pos=Program.Col.Count-1;
Employe x = new Employe();
x = (Employe)Program.Col[Program.pos];
textBox1.Text=x.num.ToString();
textBox2.Text=x.Nom.ToString();
class

public string NOM;


public int NUM;

public Employe()
{ }

public Employe(int NUM,string NOM)


{
this.NUM = NUM;
this.NOM = NOM;

1
public string Nom
{
get { return this.NOM; }
set { this.NOM = value; }
}

public int num


{
get { return this.NUM; }
set { this.NUM = value; }
}
}
}

Ajouter

try
{
SqlParameter p1 = new SqlParameter("@a", int.Parse(textBox1.Text));
SqlParameter p2 = new SqlParameter("@b", textBox2.Text);
string req = "insert into tarik values(@a,@b)";
SqlCommand cmd = new SqlCommand(req, Program.con);
Program.con.Open();
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.ExecuteNonQuery();
MessageBox.Show("ajouter est bien passee");
Program.con.Close();
}catch (SqlException x)
{
MessageBox.Show(x.Message);
}

Supprimer

try
{

SqlParameter p1 = new SqlParameter("@a",


int.Parse(textBox1.Text));
string rep = "delete tarik from tarik where num =@a";
SqlCommand cmd = new SqlCommand(rep, Program.con);
cmd.Parameters.Add(p1);
Program.con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("supprission est bien passeé");
Program.con.Close();
}
catch (SqlException x)
{
MessageBox.Show(x.Message);
}

Modifier

try
{
SqlParameter p1 = new SqlParameter("@a",
int.Parse(textBox1.Text));

2
SqlParameter p2 = new SqlParameter("@b",
textBox2.Text);
SqlParameter p3 = new SqlParameter("@c",
int.Parse(textBox3.Text));
string rep = "update tarik set num=@a,nom=@b from
tarik wher num=@c";
SqlCommand cmd = new SqlCommand(rep, Program.con);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
Program.con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("modifier est bien passeé");
Program.con.Close();
}
catch (SqlException x)
{
MessageBox.Show(x.Message);
}

Chaercher
try
{
SqlParameter p1 = new SqlParameter("@a",
int.Parse(textBox1.Text));
string rep = "select * from tarik where num=@a";
SqlCommand cmd = new SqlCommand(rep, Program.con);
Program.con.Open();
cmd.Parameters.Add(p1);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read() == true)
{
dataGridView1.Rows.Add(dr.GetValue(0).ToString(),dr.GetValue(1).ToStr
ing);
}
dr.Close();
Program.con.Close();
}
catch (SqlException x)
{
MessageBox.Show(x.Message);}
Charger Datagrideview
SqlParameter p1 = new SqlParameter("@a", int.Parse(textBox1.Text));
string req = "select * from tarik where num=@a";
SqlCommand cmd = new SqlCommand(req, Program.con);
Program.con.Open();
cmd.Parameters.Add(p1);
SqlDataReader dr = cmd.ExecuteReader() ;

while (dr.Read() == true)


{

dataGridView1.Rows.Add(dr.GetValue(0).ToString(),
dr.GetValue(1).ToString(), dr.GetValue(2).ToString(),
dr.GetValue(3).ToString(), dr.GetValue(4).ToString(),
dr.GetValue(5).ToString());
}
dr.Close();
Program.con.Close();
}

3
Remplaire combobox que vous supprim
string req = "select * from inscrept ";
SqlCommand cmd = new SqlCommand(req, Program.con);
Program.con.Open();
SqlDataReader dr = cmd.ExecuteReader(); ;
while (dr.Read() == true)
{
comboBox1.Items.Add(dr.GetValue(3).ToString());
}

Deconnecté
Program :

public static string ch = "server=.; database=gest_peche;integrated


security=true";

public static SqlConnection con = new SqlConnection(ch);


public static DataSet ds = new DataSet();
public static SqlDataAdapter da_pecheur;

load
if (Program.nbv == 0)
{
Program.nbv += 1;
//création des datatables
//création du datatable poisson
try
{
Program.da_poisson = new SqlDataAdapter("select*
from poisson", Program.con);
Program.da_poisson.Fill(Program.ds, "poisson");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "ah err au niv de la
création de Dt poisson");
}
//création de la clé primaire pour DT pecheur
try
{
DataColumn[] p1 = new DataColumn[1];
p1[0] = Program.ds.Tables[0].Columns[0];
Program.ds.Tables[0].PrimaryKey = p1;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "ah err au niv de la
création de la clé primaire pour DT pecheur");

}
//création de la clé primaire pour DT pecheur ( 2 clé primary )
try
{
DataColumn[] p3 = new DataColumn[2];
p3[0] = Program.ds.Tables[2].Columns[0];
p3[1] = Program.ds.Tables[2].Columns[1];

4
Program.ds.Tables[2].PrimaryKey = p3;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "ah err au niv de la
création de la clé primaire pour DT pecher");
//création de la relation entre les troix tables
try
{
Program.ds.Relations.Add("pecheur_pecher",
Program.ds.Tables["pecheur"].Columns[0],
Program.ds.Tables["pecher"].Columns[0]);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "err au niv de la
relation");
}

Ajouter

//ajout dans DateSet


bool tr = true;
try
{
DataRow ligne;
ligne = Program.ds.Tables["poisson"].NewRow();
ligne[0] = int.Parse(t1.Text);
ligne[1] = t2.Text;
Program.ds.Tables["poisson"].Rows.Add(ligne);
}
catch (SqlException ex)
{
MessageBox.Show("attention err au niv de la creation
du DataSet");
tr = false;
}
//ajout dans la BD
if (tr == true)
{
try
{
string req = "insert into poisson
values(@num_p,@nom_p)";
SqlParameter p1 = new SqlParameter("@num_p",
int.Parse(t1.Text));
SqlParameter p2 = new SqlParameter("@nom_p",
(t2.Text));
Program.da_poisson.InsertCommand = new
SqlCommand(req, Program.con);

Program.da_poisson.InsertCommand.Parameters.Add(p1);

Program.da_poisson.InsertCommand.Parameters.Add(p2);
Program.da_poisson.Update(Program.ds, "poisson");
MessageBox.Show("Ajout Bien passé");

5
t1.Text = " ";
t2.Text = " ";

}
catch (SqlException ex)
{
MessageBox.Show("attention err au niv de la
creation du BD" + ex.Message);
Supprimer

//suppression dans DataSet


bool tr = true;
//recherche de la ligne à supp
int pos_ex = -1;
for (int i = 0; i <= Program.ds.Tables[0].Rows.Count - 1;
i++)
{
if (int.Parse(Program.ds.Tables[0].Rows[i]
[0].ToString()) == int.Parse(t1.Text))
{
pos_ex = i;
}
}
if (pos_ex == -1)
{
tr = false;
}
else
{
//supp du DataSet
try
{
Program.ds.Tables[0].Rows[pos_ex].Delete();
}
catch (SqlException ex)
{
tr = false;
MessageBox.Show("attention err au niv da la
supp");
}
if (tr == true)
{

//supprimer de la BD
string req = "delete poisson from poisson where
num_p=@num_p";
SqlParameter p1 = new SqlParameter("@num_p",
int.Parse(t1.Text));
Program.da_poisson.DeleteCommand = new
SqlCommand(req, Program.con);

Program.da_poisson.DeleteCommand.Parameters.Add(p1);
Program.da_poisson.Update(Program.ds, "poisson");
MessageBox.Show("Suppression Bien passé");
}

}
Modifier

//rech de l'index de la ligne

6
int i, posex = -1;
for (i = 0; i < Program.ds.Tables["poisson"].Rows.Count -
1; i++)
{
if (int.Parse(Program.ds.Tables["poisson"].Rows[i]
[0].ToString()) == int.Parse(t3.Text))
{
posex = i;
}
}
bool tr = true;
//mod dand Dataset
try
{
Program.ds.Tables["poisson"].Rows[posex].BeginEdit();
Program.ds.Tables["poisson"].Rows[posex][0] =
int.Parse(t1.Text);
Program.ds.Tables["poisson"].Rows[posex][1] =
t2.Text;
Program.ds.Tables["poisson"].Rows[posex].EndEdit();
}
catch (SqlException ex)
{
tr = false;
MessageBox.Show("err au niv de la mod dans DataSet");
}
if (tr == true)
{//mod dans BD
try
{
string req = "update poisson from poisson where
num_v=@num_p,nom_v=@nom_p from poisson where num_p=@c ";
SqlParameter p1 = new SqlParameter("@num_p",
int.Parse(t1.Text));
SqlParameter p2 = new SqlParameter("@no_p",
t2.Text);
SqlParameter p3 = new SqlParameter("@c",
int.Parse(t3.Text));
Program.da_poisson.UpdateCommand = new
SqlCommand(req, Program.con);

Program.da_poisson.UpdateCommand.Parameters.Add(p1);

Program.da_poisson.UpdateCommand.Parameters.Add(p2);

Program.da_poisson.UpdateCommand.Parameters.Add(p3);
Program.da_poisson.Update(Program.ds, "poisson");
}
catch (SqlException ex)
{
MessageBox.Show("err au niv de la mod dans BD");
}

Chercher

int i, pos_ex = -1;


for (i = 0; i < Program.ds.Tables["poisson"].Rows.Count -
1; i++)
{

7
if (int.Parse(Program.ds.Tables["poisson"].Rows[i]
[0].ToString()) == int.Parse(t3.Text))
{
pos_ex = i;
}
}
if (pos_ex == -1)
{
MessageBox.Show("n'existe pas");
}
else
{
t1.Text = Program.ds.Tables["poisson"].Rows[pos_ex]
[0].ToString();
t2.Text = Program.ds.Tables["poisson"].Rows[pos_ex]
[1].ToString();
}

You might also like