Wednesday, October 27, 2010

SELECT


A select operation enables records to be retrieved from a database. When records are retrieved, they are brought back to the application and placed into memory. Because of this, a select operation contains objects that also allow these records to be stored.
It is important to note that the select operation does not modify the contents of the database, it simply retrieves data.

Variations on the SELECT SQL statement:

SELECT * FROM TableName
Returns all fields from all records

SELECT * FROM TableName WHERE ColumnID = ‘1’
Return all fields of the record that has a ColumnID of 1

SELECT FieldName1, FieldName2 FROM TableName
Returns the fields specified, from all records

SELECT FieldName1, FieldName2 FROM TableName WHERE ColumnID = ‘1’
Returns the fields specified, from the record that has a ColumnID of 1

SELECT FieldName1, FieldName2 FROM TableName WHERE ColumnID = ‘1’ AND FieldName1 = ‘X’
Returns the fields specified, from the record that has a ColumnID of 1 and FieldName1 has a value of X.
Another example which substitutes literal values for parameter values:

Public Function getCart() As Data.DataTable

    Dim myConn As SqlConnection = New SqlConnection(MyConnString)
    myConn.Open()
    Dim strSQL As String = "SELECT * FROM XYZCart"
    Dim myComm As SqlCommand = New SqlCommand(strSQL, myConn)
    Dim dsResults As New Data.DataSet
    Dim daDataAdapter As New     Data.SqlClient.SqlDataAdapter(myComm)
    daDataAdapter.Fill(dsResults, "CartResults")
    myConn.Close()
    Return dsResults.Tables("CartResults")

End Function

At the conclusion of the operation, any retrieved results are stored in the dataset.

No comments:

Post a Comment