Exercice Visual Basic : Mode connecté
Rédigé par GC Team, Publié le 07 Janvier 2012, Mise à jour le Samedi, 17 Décembre 2022 16:24![](/images/thumb/146/default.png)
Participez au vote ☆☆☆☆☆★★★★★
Ecrire le programme qui permet de réaliser l'interface suivante:
avant de commancer le programme vous devez créer une Base de donnée avec les champs cités dans l'interface
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 |
Imports System.Data.SqlClient Public Class Form2 Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Documents and Settings\ista\Bureau\application\application\database1.mdf;Integrated Security=True;User Instance=True") 'ajouter Private Sub ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click conn.Open() Dim InsertCommand As New SqlCommand InsertCommand.Connection = conn InsertCommand.CommandType = CommandType.Text InsertCommand.CommandText = "INSERT INTO Information(nom,téléphone) VALUES( Me.t2 & Me.t3 );" InsertCommand.ExecuteNonQuery() InsertCommand.Dispose() conn.Close() MsgBox(" OK ", MsgBoxStyle.Information) End Sub 'mise_à_jour Private Sub mise_à_jour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Dim MyTransaction As SqlTransaction Dim UpdateCommand As New SqlCommand Try conn.Open() MyTransaction = conn.BeginTransaction UpdateCommand.Connection = conn UpdateCommand.Transaction = MyTransaction UpdateCommand.CommandType = CommandType.Text UpdateCommand.CommandText = "mise à jour Information set nom=? WHERE Id =?;" UpdateCommand.Parameters.Clear() UpdateCommand.Parameters.AddWithValue("?", Me.t2.Text) UpdateCommand.Parameters.AddWithValue("?", Me.t1.Text) UpdateCommand.ExecuteNonQuery() UpdateCommand.CommandText = "mise à jour Information set téléphone=? WHERE Id =?;" UpdateCommand.Parameters.Clear() UpdateCommand.Parameters.AddWithValue("?", Me.t3.Text) UpdateCommand.Parameters.AddWithValue("?", Me.t1.Text) UpdateCommand.ExecuteNonQuery() MyTransaction.Commit() MsgBox(" OK ", MsgBoxStyle.Information) Catch Filed As SqlException MyTransaction.Rollback() MsgBox(Filed.Message, MsgBoxStyle.Exclamation) Finally UpdateCommand.Dispose() conn.Close() End Try End Sub 'supprimer Private Sub supprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click conn.Open() Dim DeleteCommand As New SqlCommand DeleteCommand.Connection = conn DeleteCommand.CommandType = CommandType.Text DeleteCommand.CommandText = "DELETE FROM Information WHERE id = ?;" DeleteCommand.Parameters.Clear() DeleteCommand.Parameters.AddWithValue("?", Me.t1.Text) DeleteCommand.ExecuteNonQuery() DeleteCommand.Dispose() conn.Close() MsgBox(" OK ", MsgBoxStyle.Information) End Sub 'CountId Private Sub CountId_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCountId.Click conn.Open() Dim CountIdCommand As New SqlCommand CountIdCommand.Connection = conn CountIdCommand.CommandType = CommandType.Text CountIdCommand.CommandText = "SELECT Count(id) AS CountID FROM Information;" Dim CountId As String = CountIdCommand.ExecuteScalar() MsgBox(CountId, MsgBoxStyle.Information) CountIdCommand.Dispose() conn.Close() End Sub End Class |