0% found this document useful (0 votes)
40 views22 pages

Dataacces Chsarp

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

Dataacces Chsarp

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

‫ﺗﻢ ﺗﺤﻤﻴﻞ اﻟﻤﻠﻒ ﻣﻦ ﻣﻮﻗﻊ‬

‫اﻟﺒﻮﺻﻠﺔ اﻟﺘﻘﻨﻴﺔ‬
‫‪www.boosla.com‬‬
‫الداتا أكسيس الير‬
‫يف السي شارب‬

‫‪1‬‬
!

2
o

SQl Server o

3
‫"يا أيها الرين آمنوا اتقوا اهلل و قولوا‬
‫قوال سديدا‪ .‬يصلح لكم أعمالكم و يغفس لكم‬
‫ذنوبكم ومن يطع اهلل و زسوله فقد فاش‬
‫فوشا عظيما"‬

‫األحزاب ‪ 70 :‬و ‪71‬‬

‫‪4‬‬
Stagiaire 

Groupe 
NumFiliere

5
Filiere 

Absence 
increment

create database Stagiaire


GO
use Stagiaire;

--table Filiére
create table Filiere(NumFiliere int,LibelleFiliere varchar(100),
Primary key(NumFiliere))

--table Groupe

create table Groupe (NumGroupe int,LibelleGroupe varchar(50),NumFiliere int


,Primary key(NumGroupe),Foreign key(NumFiliere) references Filiere)

--table stagiaire

create table Stagiaire(NumStagiaire int,Nom varchar(5),Prenom varchar(50),


Adresse varchar(250),DateNaissance datetime,Sexe varchar(50),NumGroupe
int,
primary key(NumStagiaire),Foreign key(NumGroupe) references Groupe)

--table Absence

create table Absence(NumAbsence int,NumStagiaire int,DateAbsence Datetime,


NbrHeurs int,Observation nvarchar(250),Primary key(NumAbsence),
Foreign key(NumStagiaire) references Stagiaire)

6
DataAccessLAyer

DataAccessLayer

7
Class

MVC (Model,View,Controller)

sql server stored procedure

8
System.Data.SqlClient,System.Data

SqlConnection

Constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
class DataAccessLayer
{
static SqlConnection SqlCon;

public DataAccessLayer()
{
SqlCon = new SqlConnection("Server=TECHNI-AMECO-
PC\\SQLEXPRESS;Database=Stagiaire;integrated security=true");
}
}
}

9
public void Connect()
{
if (SqlCon.State != ConnectionState.Open)
SqlCon.Open();
}

public void Disconnect()


{
if (SqlCon.State != ConnectionState.Closed)
SqlCon.Close();
}

public DataTable SelectData(String Stored, SqlParameter[]


Param)
{
SqlCommand SqlCmd = new SqlCommand(Stored, SqlCon);
SqlCmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Param.Length ; i++)
{
SqlCmd.Parameters.Add(Param[i]);
}
SqlDataAdapter SqlDa = new SqlDataAdapter(SqlCmd);
DataTable Dt = new DataTable();
SqlDa.Fill(Dt);
return Dt;
}

DataTable

10
Param Stored

Stored Procedure

SqlCommand

For

SqlCommand SqlDataAdapter

Datatable

public void ExecuteCommand(String Stored, SqlParameter[] Param)


{
SqlCommand SqlCmd = new SqlCommand(Stored, SqlCon);
SqlCmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Param.Length; i++)
{
SqlCmd.Parameters.Add(Param[i]);
}
SqlCmd.ExecuteNonQuery();
}

11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
class DataAccessLayer
{
static SqlConnection SqlCon;

public DataAccessLayer()
{
SqlCon = new SqlConnection("Server=TECHNI-AMECO-
PC\\SQLEXPRESS;Database=Stagiaire;integrated security=true");
}
public void Connect()
{
if (SqlCon.State != ConnectionState.Open)
SqlCon.Open();
}

public void Disconnect()


{
if (SqlCon.State != ConnectionState.Closed)
SqlCon.Close();
}

public DataTable SelectData(String Stored, SqlParameter[] Param)


{
SqlCommand SqlCmd = new SqlCommand(Stored, SqlCon);
SqlCmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Param.Length ; i++)
{
SqlCmd.Parameters.Add(Param[i]);
}
SqlDataAdapter SqlDa = new SqlDataAdapter(SqlCmd);
DataTable Dt = new DataTable();
SqlDa.Fill(Dt);
return Dt;
}

12
public void ExecuteCommand(String Stored, SqlParameter[] Param)
{
SqlCommand SqlCmd = new SqlCommand(Stored, SqlCon);
SqlCmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Param.Length; i++)
{
SqlCmd.Parameters.Add(Param[i]);
}
SqlCmd.ExecuteNonQuery();
}

}
}

Absence

public DataTable SelectAbsence(int NumStagiaire)


{
DataAccessLayer Dal = new DataAccessLayer();
DataTable Dt = new DataTable();
SqlParameter[] Param=new SqlParameter[0];
try
{
Param[0] = new SqlParameter("NumStg", SqlDbType.Int);
Param[0].Value = NumStagiaire;
Dal.Connect();
Dt=Dal.SelectData("SP_SelectAbsence", Param);
}
catch
{
Dal.Disconnect();
}
return Dt;
}

13
Stored Procedure SP_SelectAbsence

create proc SP_SelectAbsence


@NumStg int
as
select DateAbsence,NbrHeurs,Observation
from Absence where NumStagiaire=@NumStg

Absence

create proc SP_AddAbsence


@NumStg int,@DateAbsence datetime,@NbrHeur int,@Observation nvarchar(250)
as
insert into absence(NumStagiaire,DateAbsence,NbrHeurs,Observation)
values(@NumStg,@DateAbsence,@NbrHeur,@Observation)

Absence

Absence

14
public void AddAbsence(int NumStagiaire, DateTime DateAbsence, int
NbrHeurs, String Observation)
{
DataAccessLayer Dal = new DataAccessLayer();
SqlParameter[] Param = new SqlParameter[4];
try
{
Param[0] = new SqlParameter("NumStg", SqlDbType.Int);
Param[0].Value = NumStagiaire;
Param[1] = new SqlParameter("DateAbsence",
SqlDbType.DateTime);
Param[1].Value = DateAbsence;
Param[2] = new SqlParameter("NbrHeur", SqlDbType.Int);
Param[2].Value = NbrHeurs;
Param[3] = new SqlParameter("Observation",
SqlDbType.NVarChar);
Param[3].Value = Observation;
Dal.Connect();
Dal.ExecuteCommand("SP_AddAbsence", Param);
}
catch
{
Dal.Disconnect();
}
}

Absence

15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DataAccessLayer
{
class Absence
{
public DataTable SelectAbsence(int NumStagiaire)
{
DataAccessLayer Dal = new DataAccessLayer();
DataTable Dt = new DataTable();
SqlParameter[] Param=new SqlParameter[1];
try
{
Param[0] = new SqlParameter("NumStg", SqlDbType.Int);
Param[0].Value = NumStagiaire;
Dal.Connect();
Dt=Dal.SelectData("SP_SelectAbsence", Param);
}
catch
{
Dal.Disconnect();
}
return Dt;
}

16
public void AddAbsence(int NumStagiaire, DateTime DateAbsence, int
NbrHeurs, String Observation)
{
DataAccessLayer Dal = new DataAccessLayer();
SqlParameter[] Param = new SqlParameter[4];
try
{
Param[0] = new SqlParameter("NumStg", SqlDbType.Int);
Param[0].Value = NumStagiaire;
Param[1] = new SqlParameter("DateAbsence",
SqlDbType.DateTime);
Param[1].Value = DateAbsence;
Param[2] = new SqlParameter("NbrHeur", SqlDbType.Int);
Param[2].Value = NbrHeurs;
Param[3] = new SqlParameter("Observation",
SqlDbType.NVarChar);
Param[3].Value = Observation;
Dal.Connect();
Dal.ExecuteCommand("SP_AddAbsence", Param);
}
catch
{
Dal.Disconnect();
}
}

}
}

17
Ajouter

Absence Ab = new Absence();


Ab.AddAbsence(int.Parse(TxtID.Text),
DateAbsence.Value,int.Parse(TxtNbr.Text), TxtObserv.Text);

18
Absence Ab = new Absence();
DataTable Dt = new DataTable();
Dt=Ab.SelectAbsence(int.Parse(TxtSearch.Text));
this.DGV.DataSource = Dt;

Ab

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;
namespace DataAccessLayer
{
public partial class Form1 : Form
{
Absence Ab = new Absence();
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
Ab.AddAbsence(int.Parse(TxtID.Text), DateAbsence.Value,
int.Parse(TxtNbr.Text), TxtObserv.Text);
}

19
private void button1_Click(object sender, EventArgs e)
{
DataTable Dt = new DataTable();
Dt=Ab.SelectAbsence(int.Parse(TxtSearch.Text));
this.DGV.DataSource = Dt;
}
}
}

20
[email protected]

21

You might also like