Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

VB Dot Net VERSION COMPLETE CODES SOURCES

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1sur 6

Code Dot Net : Connexion. 2021/2022 Formateur : L.KARIM.

Codes Connexion Entre PC (Local=Client) et PC(Serveur)


L’application et la base de données
Version 2021 / 2022
Phase 1
Zone Importation (Au Début de la page de code)
‘Fournisseur MS Access
Imports System.Data.OleDb

‘Fournisseur MS SqlServeur
Imports System.Data.SqlClient
Phase 2
Zone Décalaration (Déclaration Au Début de la classe)
‘Déclaration Pour Fournisseur MS Access
Private con As New OleDbConnection
Private dtadapter As New OleDbDataAdapter("select *from EMPLOYE", con)
Private dtset As New DataSet

‘Déclaration Pour Fournisseur MS SqlServeur


Private con As New SqlConnection("Initial Catalog=Nom de la base de données;Data
Source=(local)\sqlexpress;Integrated Security=SSPI;")
Private dtadapter As New SqlDataAdapter("select *from EMPLOYE", con)
Private dtset As New DataSet

Phase 3
Inserer les Codes de connexion dans une Forme Evénement Load
'Connexion avec Base De Données (Localisée Bin) ACCDB(2007+)
‘Ligne con.ConnectionString uniquement Pour Fournisseur MS Access
con.ConnectionString = "provider=microsoft.Ace.oledb.12.0;data source= bibnet.accdb"
con.Open()
'Identification Table ou requête Exemple la Table T1
dtadapter.Fill(dtset, "EMPLOYE")
'Remplissage GridView par le Contenu de la table T1 (Nomée Grid)
G_EMPLOYE.DataSource = dtset.Tables("EMPLOYE")
con.Close()
Codes Effaçer (Bouton)
L’application et la base de données
'Bouton Effaçer
C_ID.Text = Nothing
T_Nom.Text = Nothing
T_Prenom.Text = Nothing
Insérer un message comme boite de confirmation
Etape 1 : Zone Déclaration
Dim x As DialogResult
Etape 2 : Zone Code du Bouton
x = MessageBox.Show("Voulez Vous Vraiment ", "Gestion ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If x = MsgBoxResult.Yes Then
‘Votre Bloc de Codes
End If

Codes Bouton Ajouter (Bouton Enregistrer)


L’application et la base de données
'Bouton Ajout
Dim sw As Integer = 0
For s = 0 To Me. G_EMPLOYE.RowCount - 2
If C_ID.Text = Me. G_EMPLOYE.Item(0, s).Value Then
sw = 1
s= Me. G_EMPLOYE.RowCount - 2
End If
Next
If sw = 0 Then
If C_ID.Text <> "" And T_Nom.Text <> "" And T_Prenom.Text <> "" Then
Dim NewLigne As DataRow
' Création de la nouvelle ligne
NewLigne = dtset.Tables("EMPLOYE").NewRow
'affectation des valeurs

1
Code Dot Net : Connexion. 2021/2022 Formateur : L.KARIM.
NewLigne(0) = C_ID.Text
NewLigne(1) = T_Nom.Text
NewLigne(2) = T_PRENOM.Text
' Ajout de la ligne à la table
dtset.Tables("EMPLOYE").Rows.Add(NewLigne)
' Création CommandBuilder
'(genere automatiquement l'update entre le dataSet et la base de donnée
Dim CmdBuild As OleDb.OleDbCommandBuilder
CmdBuild = New OleDb.OleDbCommandBuilder(dtadapter)
dtadapter.InsertCommand = CmdBuild.GetInsertCommand
dtadapter.Update(dtset, " EMPLOYE")
MsgBox("Ajout Avec Succes", MsgBoxStyle.Information)
con.Close()
Else
MsgBox("Vous Devez Remplir Tous Les Champs SVP", MsgBoxStyle.Exclamation)
End If
Else
MsgBox("ID Existe Déjà", MsgBoxStyle.Critical)
End If

Recherche Unique (Quatre Méthodes)


Codes Recherché (Consultation)
L’application et la base de données
Méthode 1 : Contrôle TextBox (Bouton Evénement Click)
Dim sw As Integer = 0
For s = 0 To Me.G_EMPLOYE.RowCount - 2
If C_ID.Text = Me.G_EMPLOYE.Item(0, s).Value Then
sw = 1
T_NOM.Text = Me.G_EMPLOYE.Item(1, s).Value
T_PRENOM.Text = Me.G_EMPLOYE.Item(2, s).Value
End If
Next

If sw = 0 Then
MsgBox("ID Introuvable", MsgBoxStyle.Critical)
End If

Méthode 2 : Boite de Message InputBox (Bouton Evénement Click)


Dim a As Integer
Dim sw As Integer = 0
a = InputBox("Entrer ID Demandé", "Projet BIBnet")

For s = 0 To Me.G_EMPLOYE.RowCount - 2
If a = Me.G_EMPLOYE.Item(0, s).Value Then
sw = 1
C_ID.Text = Me.G_EMPLOYE.Item(0, s).Value
T_NOM.Text = Me.G_EMPLOYE.Item(1, s).Value
T_PRENOM.Text = Me.G_EMPLOYE.Item(2, s).Value
End If
Next

If sw = 0 Then
MsgBox("ID Introuvable", MsgBoxStyle.Critical)
End If
Méthode 3 : Combo Box (Evénement C_ID_SelectedIndexChanged)
N.B. : Avant de saisir le Code de cette méthode veuillez saisir le code de rechargement des
élèments du Combo lors du démarrage de la Forme.

Etape 1 : Form_Load
'Rechargement Combo : pour la Préparer à la recherche.
For s = 0 To Me.G_EMPLOYE.RowCount - 2
C_ID.Items.Add(Me.G_EMPLOYE.Item(0, s).Value)
Next
Etape 2 : C_ID_SelectedIndexChanged
If C_ID.Text <> "" Then
For s = 0 To Me.G_EMPLOYE.RowCount - 2
If C_ID.Text = Me.G_EMPLOYE.Item(0, s).Value Then
T_NOM.Text = Me.G_EMPLOYE.Item(1, s).Value
T_PRENOM.Text = Me.G_EMPLOYE.Item(2, s).Value

2
Code Dot Net : Connexion. 2021/2022 Formateur : L.KARIM.
End If
Next
End If
Méthode 4 : Grid (Evénement Grid_CellClick)
Dim ligneencours As Integer
ligneencours = G_EMPLOYE.CurrentRow.Index
C_ID.Text = G_EMPLOYE.Item(0, ligneencours).Value
T_NOM.Text = G_EMPLOYE.Item(1, ligneencours).Value
T_PRENOM.Text = G_EMPLOYE.Item(2, ligneencours).Value

Recherche Par Lot (Trois Méthodes)


Codes Recherché (Consultation)
L’application et la base de données
Méthode 1 : Contrôle TextBox (Bouton Evénement Click)

Dim x As Integer
Dim sw As Integer
sw = 0
x = 0
GridText.Rows.Clear()
For s = 0 To Form1.Grid.RowCount - 2
If Txt.Text = Form1.Grid.Item(0, s).Value Then
sw = 1
Me.GridText.Rows.Add()
Me.GridText.Item(0, x).Value = Form1.Grid.Item(0, s).Value
Me.GridText.Item(1, x).Value = Form1.Grid.Item(1, s).Value
Me.GridText.Item(2, x).Value = Form1.Grid.Item(2, s).Value
x = x + 1
End If
Next
If sw = 0 Then
MsgBox("Référence Introuvable", MsgBoxStyle.Critical)
End If
Méthode 2 : Boite de Message InputBox (Bouton Evénement Click)
Dim a As Integer
Dim x As Integer
Dim sw As Integer
sw = 0
x = 0
a = InputBox("Entrer la Référence Demandé", "Projet Test")
Gridinput.Rows.Clear()
For s = 0 To Form1.Grid.RowCount - 2
If a = Form1.Grid.Item(0, s).Value Then
sw = 1
Me.Gridinput.Rows.Add()
Me.Gridinput.Item(0, x).Value = Form1.Grid.Item(0, s).Value
Me.Gridinput.Item(1, x).Value = Form1.Grid.Item(1, s).Value
Me.Gridinput.Item(2, x).Value = Form1.Grid.Item(2, s).Value
x = x + 1
End If
Next
If sw = 0 Then
MsgBox("Référence Introuvable", MsgBoxStyle.Critical)
End If
Méthode 3 : Combo Box (Evénement C1_SelectedIndexChanged)
Dim x As Integer
x = 0
C1.Items.Clear()
Grid.Rows.Clear()
For s = 0 To Form1.Grid.RowCount - 2
If C1.Text = Form1.Grid.Item(0, s).Value Then
Me.Grid.Rows.Add()
Me.Grid.Item(0, x).Value = Form1.Grid.Item(0, s).Value
Me.Grid.Item(1, x).Value = Form1.Grid.Item(1, s).Value
Me.Grid.Item(2, x).Value = Form1.Grid.Item(2, s).Value
x = x + 1
End If
C1.Items.Add(Form1.Grid.Item(0, s).Value)
Next

3
Code Dot Net : Connexion. 2021/2022 Formateur : L.KARIM.
Codes Modification (Bouton Modification)
L’application et la base de données
« Base de Données Local »
If MsgBox("voulez vous vraiment Modifier cet enregistrement", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
Dim sw As Integer = 0
Dim x As Integer
For s = 0 To Me.G_EMPLOYE.RowCount - 2
If C_ID.Text = Me.G_EMPLOYE.Item(0, s).Value Then
sw = 1
x = s
s = Me.G_EMPLOYE.RowCount - 2
End If
Next
If sw = 1 Then
Dim ligneencours As Integer
ligneencours = x
Dim cle As String
cle = G_EMPLOYE.Item(0, ligneencours).Value
Dim matable As DataTable
matable = dtset.Tables("EMPLOYE")
Dim laligne As DataRow()
laligne = matable.Select("ID=" & cle)
laligne(0).Item(1) = T_NOM.Text
laligne(0).Item(2) = T_PRENOM.Text
base(dtadapter, "EMPLOYE")
Else
MsgBox("ID N’Existe Pas", MsgBoxStyle.Critical)
End If
End If

Codes Procédure Base


« Action de modification Base de Données à distance»
Private Sub base(ByVal adapter As OleDbDataAdapter, ByVal table As String)
' con.Open()
Dim cmdbuilder As OleDbCommandBuilder
cmdbuilder = New OleDb.OleDbCommandBuilder(adapter)
adapter.UpdateCommand = cmdbuilder.GetUpdateCommand
adapter.Update(dtset, table)
End Sub

Codes Suppression
L’application et la base de données
'Bouton Suppression
x = MessageBox.Show("Voulez Vous Vraiment Supprimer Cet Enregistrement", "Gestion BIBNET",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If x = MsgBoxResult.Yes Then
Dim x As Integer
For s = 0 To Me.G_EMPLOYE.RowCount - 2
If C_ID.Text = Me.G_EMPLOYE.Item(0, s).Value Then
x = s
End If
Next

Dim ligneencours As Integer


ligneencours = x
Dim cle As String
cle = G_EMPLOYE.Item(0, ligneencours).Value
Dim ligne As DataRow()
ligne = dtset.Tables("EMPLOYE").Select("ID=" & cle)
ligne(0).Delete() 'Code uniquement pour la suppression dans la base de donnée locale
C_ID.Text = Nothing
T_NOM.Text = Nothing
T_PRENOM.Text = Nothing
MsgBox("Suppression Effectuée dans la base de données Locale", MsgBoxStyle.Information)
' con.Open()

4
Code Dot Net : Connexion. 2021/2022 Formateur : L.KARIM.
'ici nous allons ouvrir la connexion pour accéder et utiliser la base de données distante
Dim cmdbuilder As OleDbCommandBuilder
cmdbuilder = New OleDb.OleDbCommandBuilder(dtadapter)
dtadapter.DeleteCommand = cmdbuilder.GetDeleteCommand
dtadapter.Update(dtset, "EMPLOYE")
MsgBox("Suppression Effectuée dans la base de données Distante", MsgBoxStyle.Information)
End If
con.Close()

Exploitation Photo Dans une Table & Interface


AJOUT & MODIFICATION
Phase 1
Insérer un Champ Dans la Table Avec Type ObjetOLE
Phase 2 Code à Insérer au début de la Page de Codes.
Imports System.Drawing.Image
Imports System.IO
Phase 3 Code à Insérer Form_Load
N.B. :Avant d’insérer ce bloc de codes veuillez spécifier au niveau propriètés une image de
BackgroundImage au contrôle PictureBox1
PictureBox1.Image = PictureBox1.BackgroundImage
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
Phase 4 Code à Insérer Bouton Enregistrer (Ajout + Modification)
NewLigne(N°Champ) = ConvertToData(Me.PictureBox1.Image)
N.B. : ConvetToData est une Fonction que vous devez la créer (Phase 5)
Phase 5 Fonction ConvetToData
Public Function ConvertToData(ByVal myImage As Image) As Byte()
Dim ms As New MemoryStream()
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim myBytes(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(myBytes, 0, ms.Length)
Return myBytes
End Function
Phase 6 Création de Bouton Parcourir
1. Insérer le Contrôle OpenFileDialog
2. Inserer le Bloc de Code Suivant (Bouton Parcourir)
Me.OpenFileDialog1.FileName = Nothing
Me.OpenFileDialog1.ShowDialog()
If Not Me.OpenFileDialog1.FileName = Nothing Then
Me.PictureBox1.ImageLocation = Me.OpenFileDialog1.FileName
End If
Exploitation Photo Dans une Table & Interface
RECHERCHE
Phase 1 Insérer ce Bloc de Code Evénement Recherche n’importe quelle Méthode.
PictureBox1.Image = ConvertToImage(Grid1.Item(1, ligneencours).Value)
Phase 2 Créer la Fonction Permettant de Convertir le Type Data Vers le Type
Image.
Public Function ConvertToImage(ByVal data() As Byte) As Image
Dim stream As New MemoryStream(data)
Return Image.FromStream(stream)
End Function

Utilisation de la Fonction RND


Dim rnd As Random
Dim number As Integer
rnd = New Random
number = rnd.Next(999, 10000)
‘code pour affecter le numéro RND à un Champ de la Table
NewLigne(N°Champ) = number.ToString

Activation Son Audio


5
Code Dot Net : Connexion. 2021/2022 Formateur : L.KARIM.
My.Computer.Audio.Play(My.Resources.Nom Fichier Audio Wave, AudioPlayMode.Background)

http://www.universal-soundbank.com/
E.Mail
L’application et la base de données
Phase 1 Au Début Insérer :
Imports System.Net.Mail

Phase 2 Insérer le Bloc de code Evénement envoyer :


Dim mail As New MailMessage()
Dim smtpserver As New SmtpClient
‘T1.text Contient Adresse E.Mail Application / T2.Text le Mot de Passe E.Mail
smtpserver.Credentials = New Net.NetworkCredential(T1.Text, T2.Text)
smtpserver.Port = 587
‘Le Compte Gmail comme Source d’envoi
smtpserver.Host = "smtp.gmail.com"
smtpserver.EnableSsl = True
‘T3.text Contient Adresse E.Mail Destinataire
mail.To.Add(T3.Text)
mail.From = New MailAddress(T3.Text)
‘T4.text Contient Objet Envoi Destinataire
mail.Subject = T4.Text
‘T5.text Contient Contenu E.Mail Destinataire
mail.Body = T5.Text
smtpserver.Send(mail)
MsgBox("Votre Message est Envoyé", MsgBoxStyle.Information)

Générer un QR Code
‘Ligne De Code création QR Code
Qr1.Text = "Nom :" + T1.Text() & vbCrLf & "Prénom :" + T2.Text() & vbCrLf & "CIN :" + T3.Text()

Impression
1. Insérer les contrôles PrintDialog1 et PrintDocument1

2. PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
PrintDocument1.Print()
End If

Vous aimerez peut-être aussi