1 Dim conn As New SqlConnection("Data Source=(Local);Initial Catalog=AddressBook;Integrated Security=SSPI")
2 Dim command As New SqlCommand()
3
4 command.Connection = conn
5
6 command.Parameters.AddWithValue("@Title", txtTitle.Text)
7 command.Parameters.AddWithValue("@FirstName", txtFirstName.Text)
8 command.Parameters.AddWithValue("@LastName", txtLastName.Text)
9
10 'Determine if we are adding a new record or editing an
11 'existing record based on the value of the _contactID
12 'variable
13 If _contactID = 0 Then
14 command.CommandText = "Contacts_Insert"
15 Else
16 command.CommandText = "Contacts_Update"
17 command.Parameters.AddWithValue("@ContactID", _contactID)
18 End If
19
20 command.CommandType = CommandType.StoredProcedure
21
22 'Open the connection to the database and execute the
23 'SQL Statement
24 conn.Open()
25
26 'Dim command As New SqlCommand(sql, conn)
27 command.ExecuteNonQuery()
28
29 conn.Close()
30
31 'Close the form.
32 Me.Close()
The code above works similarly to the previous version we used. However, instead of creating two different SQL Statements based on whether we were updating an existing record or creating a new one, this code simply calls a different procedure. You will also note that we add the common parameters to the Command’s Parameter collection in lines 6 – 8 and only if we are updating an existing record do we add a further parameter on line 17.
The final routine to replace is the RetrieveContactDetails routine:
1 'Retrieves a record from the Contacts table based
2 'on the _contactID value.
3 Dim conn As New SqlConnection("Data Source=(Local);Initial Catalog=AddressBook;Integrated Security=SSPI")
4
5 Dim command As New SqlCommand("Contacts_Contact_Select", conn)
6
7 command.CommandType = CommandType.StoredProcedure
8 command.Parameters.AddWithValue("@ContactID", _contactID)
9
10 'Open the connection to the database
11 conn.Open()
12
13 'Use the Command object's ExecuteReader method which
14 'will return an SqlDataReader object that we can use
15 'for forward only access through the returned data.
16 Dim contactReader As SqlDataReader = command.ExecuteReader
17
18 'There should only be one row returned due to the WHERE
19 'clause retrieving a specific ContactID. So initiate the Read
20 'method of the contactReader and read the field values:
21 contactReader.Read()
22 txtTitle.Text = contactReader("Title").ToString
23 txtFirstName.Text = contactReader("FirstName").ToString
24 txtLastName.Text = contactReader("LastName").ToString
25
26 'Close the connection to the database
27 conn.Close()
Again, this routine is very similar to the previous routine, the only real changes to this routine are found on lines 5 – 8 where the command is created and the @ContactID parameter is added to the parameters collection of the command object.
At this point, the program should now work exactly as it did before but now we are targeting a Microsoft SQL Server database and using Stored Procedures to manage the data in the Contacts table.