Using a Parameterized Query in VB



Using a Parameterized Query in VB .Net

Using a parameterized query was explained in detail in your text. The problem that some of you may be having is that it does not explain fully how to use the parameterized query with tiered applications.

For this example we will be using the customer table in the hardware database. The purpose of this example is to allow someone to find a customer in the database using the customer’s first Name as part of the search criteria.

The following code would appear in the data tier of your application called FindCust.vb

Public Function FindCust(ByVal FirstName As String) As DataSet

‘The data Adapter holds all the SQL statements for updating, deleting, Selecting

‘etc. @FName is the parameter name I gave in the query builder

daCustomer.SelectCommand.Parameters(“@FName”).Value = FirstName

‘After setting the parameter fill the dataset

daCustomer.Fill(dsCustomer, “Customer”)

‘The dataset will now hold all the rows returned from the query

return dsCustomer

End Function

In the form probably in the button event handler you may want to first check to see if the number of rows returned is greater than 0

With this code:

Dim LastName As String

If dsCustomer.Tables(“Customer”).Rows.Count = 0 Then

MessageBox.show(“Customer not found”)

Else

LastName = dsCustomer.Tables(“Customer”).Row(0).Item(2)

End If

You will now be able to check to see if the query returned any results. If the results are returned you can pull out of the first row (0) third column (Item(2)) for the customer’s the last name. Be careful with this, the column that you are getting the information from does not have the same number as it appears in the database. Everything is zero based and therefore if Last Name is in Column three in your database the index in the Item function will get the number 2.

If you have any further questions please feel free to e-mail me

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download