Private Shared ReadOnly _connectionString As String
Private _id As Integer
Private _name As String
Private _age As String
Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As String
Get
Return _age
End Get
Set(ByVal value As String)
_age = value
End Set
End Property
Public Sub Delete(ByVal id As Integer)
Dim con As New SqlConnection(_connectionString)
Dim cmd As New SqlCommand("DELETE tblPeople WHERE Id=@Id", con)
cmd.Parameters.AddWithValue("@Id", id)
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
Public Function GetAll() As List(Of People)
Dim results As List(Of People) = New List(Of People)()
Dim con As New SqlConnection(_connectionString)
Dim cmd As New SqlCommand("SELECT id,name,age FROM tblPeople", con)
Using con
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
Do While dr.Read()
Dim newPerson As New People()
newPerson.Id = CInt(Fix(dr("Id")))
newPerson.Name = CStr(dr("Name"))
newPerson.Age = CStr(dr("Age"))
results.Add(newPerson)
Loop
End Using
Return results
End Function
Shared Sub New()
_connectionString = WebConfigurationManager.ConnectionStrings("connectionString").ConnectionString
End Sub
End Class