ASP.Net - Using Sql objects to access data in a database

Here is an example of retreiving data from a database using one parameter

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
''' <summary>
''' Get's the id associated with the code
''' </summary>
''' <param name="code"></param>
''' <returns>
''' an integer if the code being request was found.
''' 0 if the code was not found
''' </returns>
Public Shared Function GetID(code As String) As Integer
Using connection As SqlConnection _
= New SqlConnection(ConnectSP.GetConnectionString())

Dim sqlStatement As String _
= "select ID " _
& "from Admin_Analysis_Type " _
& "where code=@code"

Using command As SqlCommand = New SqlCommand(sqlStatement, connection)
command.Parameters.Add(New SqlParameter("@code", SqlDbType.VarChar, 50)).Value = code

Using datareader As SqlDataReader = command.ExecuteReader()
If datareader.Read() Then
Return datareader.GetValue(0)
End If
End Using
End Using
End Using
Return 0
End Function