Public Class AdoNet
Implements IDisposable
Private con As SqlClient.SqlConnection
Private com As SqlClient.SqlCommand
Private DatRead As SqlClient.SqlDataReader
Private P_SourceDonnee As String
Private P_NomBD As String
Private P_ConnectionString As String
Private P_Action As String
Private P_SqlSelect As String
Private P_SqlInsert As String
Private P_SqlUpdate As String
Private P_SqlDelete As String
Public ReadOnly Property GetSourceDeDonnee()
Get
Return P_SourceDonnee
End Get
End Property
Public ReadOnly Property GetNomBD()
Get
Return P_NomBD
End Get
End Property
Public ReadOnly Property GetAction()
Get
Return (P_Action)
End Get
End Property
Public Property SqlSelect() As String
Get
Return P_SqlSelect
End Get
Set(ByVal Value As String)
P_SqlSelect = Value
End Set
End Property
Public Property SqlInsert() As String
Get
Return P_SqlInsert
End Get
Set(ByVal Value As String)
P_SqlInsert = Value
End Set
End Property
Public Property SqlUpdate() As String
Get
Return P_SqlUpdate
End Get
Set(ByVal Value As String)
P_SqlUpdate = Value
End Set
End Property
Public Property SqlDelete() As String
Get
Return P_SqlDelete
End Get
Set(ByVal Value As String)
P_SqlDelete = Value
End Set
End Property
Public Sub New(ByVal SourceDeDonnee As String, _
ByVal NomBD As String, _
Optional ByVal SqlSelect As String = "", _
Optional ByVal SqlInsert As String = "", _
Optional ByVal SqlUpdate As String = "", _
Optional ByVal SqlDelete As String = "")
If SqlSelect = "" And SqlInsert = "" And SqlUpdate = "" And SqlDelete = "" _
Or NomBD = "" Or SourceDeDonnee = "" Then Me.Dispose()
Try
Dim constr As String
P_SourceDonnee = SourceDeDonnee
P_NomBD = NomBD : P_Action = "Connection"
P_SqlSelect = SqlSelect : P_SqlInsert = SqlInsert
P_SqlUpdate = SqlUpdate : P_SqlDelete = SqlDelete
constr = "Data Source = " & SourceDeDonnee & " ;Initial Catalog = " & NomBD & " ;Integrated Security=SSPI"
con = New SqlClient.SqlConnection(constr)
P_ConnectionString = constr
con.Open()
Catch ex As Exception
MsgBox(ex.Message)
Me.Dispose()
End Try
End Sub
Public Function Trouver_Ligne(ByVal ChampRecherche As String, ByVal ValeurRecherche As String, _
ByVal ChampNumerique As Boolean, Optional ByVal SqlSelect As String = "") As SqlClient.SqlDataReader
If SqlSelect = "" And P_SqlSelect = "" Then _
Me.Dispose()
If ChampRecherche = "" Or ValeurRecherche = "" Then _
Me.Dispose()
If Not SqlSelect = "" Then P_SqlSelect = SqlSelect
If Not DatRead Is Nothing Then _
If Not DatRead.IsClosed Then DatRead.Close()
Try
Dim Commande As String
If ChampNumerique Then Commande = _
P_SqlSelect & " where " & ChampRecherche & " = '" & ValeurRecherche & "'" _
Else Commande = _
P_SqlSelect & " where " & ChampRecherche & " like '" & ValeurRecherche & "'"
com = New SqlClient.SqlCommand(Commande, con) : P_Action = "Select"
DatRead = com.ExecuteReader()
Return DatRead
Catch ex As Exception
Me.Dispose()
End Try
End Function
Public Function ExecuteSelectCommand(Optional ByVal SqlSelect As String = "") As SqlClient.SqlDataReader
If SqlSelect = "" And P_SqlSelect = "" Then _
Me.Dispose()
If Not SqlSelect = "" Then P_SqlSelect = SqlSelect
If Not DatRead Is Nothing Then
DatRead.Close()
End If
Try
Dim Commande As String = P_SqlSelect
com = New SqlClient.SqlCommand(Commande, con) : P_Action = "Select"
DatRead = com.ExecuteReader()
Return DatRead
Catch ex As Exception
Me.Dispose()
End Try
End Function
Public Sub MettreAjour(Optional ByVal SqlUpdate As String = "")
If SqlUpdate = "" And P_SqlUpdate = "" Then _
Me.Dispose()
If Not SqlUpdate = "" Then P_SqlUpdate = SqlUpdate
Try
com = New SqlClient.SqlCommand(SqlUpdate, con)
com.CommandType = CommandType.Text
com.CommandTimeout = 5 : P_Action = "Update"
com.ExecuteNonQuery()
Catch ex As Exception
Me.Dispose()
End Try
End Sub
Public Sub InsererLigne(Optional ByVal SqlInsert As String = "")
If SqlInsert = "" And P_SqlInsert = "" Then _
Me.Dispose()
If Not SqlInsert = "" Then P_SqlInsert = SqlInsert
Try
com = New SqlClient.SqlCommand(SqlInsert, con)
com.CommandType = CommandType.Text
com.CommandTimeout = 5 : P_Action = "Insert"
Catch ex As Exception
Me.Dispose()
End Try
End Sub
Public Sub SupprimerLigne(Optional ByVal SqlDelete As String = "")
If SqlDelete = "" And P_SqlDelete = "" Then _
Me.Dispose()
If Not SqlDelete = "" Then P_SqlDelete = SqlDelete
Try
com = New SqlClient.SqlCommand(SqlDelete, con)
com.CommandType = CommandType.Text
com.CommandTimeout = 5 : P_Action = "Delete"
Catch ex As Exception
Me.Dispose()
End Try
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
con.Close() : con = Nothing
DatRead = Nothing : com = Nothing
End Sub
End Class