Example Usage Sql Select Statement in Visual Basic 2008

On this occasion we will discuss about the use of one of the sql commands that select statement, select statement is used to select or choose download the contents of a table in a database (still using Access database), the use of the select syntax is as follows:

SELECT [predicate] { * | table.* | [table.]field1 [AS alias1] [, [table.]field2 [AS alias2] [, ...]]}
FROM tableexpression [, ...] [IN externaldatabase]
[WHERE... ]
[GROUP BY... ]
[HAVING... ]
[ORDER BY... ]
[WITH OWNERACCESS OPTION]


The explanation for each section are:

Predicate
Example predicate: ALL, DISTINCT, DISTINCTROW, or TOP. Predicate is used to limit the number of records returned when the select command is executed, if one of the above predicate is not specified, then the default is ALL.

*
Used if you want to bring up all of the fields in the table.

Table
Name of the table containing the fields of the record to be selected.

field1, field2
Field name that contains the data that you will choose

alias1, alias2
The name is used as the column header, not the original name of the table
 
tableexpression
Table names that contain the data that you will choose
 
externaldatabase
The database that contains the table name in tableexpression if not contained in the current database
Google Translate for Business:Translator ToolkitWebsite TranslatorGlobal Market Finder.



To better understand the select command statement, let's use the example project from our previous post is How to Display Content In ListView Into Database In Vb.Net.

1. Add 7 pieces of control radiobutton, 3 datetimepicker object, 4 for the textbox and a combobox, set the position in accordance with the drawings :
visual basic
2. This form of design, we will achieve that goal are :
  • If the radiobutton "JoinDate, From" is selected, then the program will select its contents JoinDate table according to the criteria we choose the date.
  • If the radiobutton "From JoinDate Year" is selected, then the program will select the contents of the table are the same as in the year selected.
  • If the radiobutton "Age =" selected, then the program will select the contents of the table are the same age age age is entered in the text.
  • If the radiobutton "Name" is selected, then the program will select the contents of a table whose name contains words that we type in the name text.
  • If the radiobutton "Certificate" is selected, then the program will select the contents of the certificate is the same table with options in the combobox.
  • If the radiobutton "She's In The Employee Jlh>" is selected, then the program will display the number of employees older than age we type in text age.
  • If the radiobutton "All" is selected, then the program will select all the contents of the table.
3. To make coding in accordance with the above command, double-click the search button, and type the following command :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Using Conn As New OleDbConnection(ConnStr)
Conn.Open()
Dim vCari As String
If RadioButton7.Checked = True Then
vCari = "Select * From TestTable"
ElseIf RadioButton1.Checked = True Then
vCari = "Select * From TestTable Where JoinDate>=Cdate('" & DtJoin1.Text & "') And " & _
"JoinDate<=Cdate('" & DtJoin2.Text & "')"
ElseIf RadioButton2.Checked = True Then
vCari = "Select * From TestTable Where Year(JoinDate)=" & DtYear.Text & ""
ElseIf RadioButton3.Checked = True Then
vCari = "Select * From TestTable Where EmployeeAge=" & TxtAge.Text & ""
ElseIf RadioButton4.Checked = True Then
vCari = "Select * From TestTable Where EmployeeName Like '%" & TxtName.Text & "%'"
ElseIf RadioButton5.Checked = True Then
vCari = "Select * From TestTable Where Certificate=" & CmbCertificate.Text & ""
ElseIf RadioButton6.Checked = True Then
vCari = "Select Count(EmployeeName) As Total From TestTable Where EmployeeAge>" & TxtAge1.Text & ""
End If
Using Cmd As New OleDbCommand(vCari, Conn)
Using Data As OleDbDataReader = Cmd.ExecuteReader
Dim x As Integer = 0
ListView1.Items.Clear()
While Data.Read
If RadioButton6.Checked = False Then
ListView1.Items.Add(Data("ID"))
ListView1.Items(x).SubItems.Add(Data("EmployeeName"))
ListView1.Items(x).SubItems.Add(Data("EmployeeAddress"))
ListView1.Items(x).SubItems.Add(Data("EmployeeAge"))
ListView1.Items(x).SubItems.Add(Data("JoinDate"))
ListView1.Items(x).SubItems.Add(Data("Certificate"))
x = x + 1
Else
TxtOrg.Text = Data("Total")
End If

End While
End Using
End Using
End Using
End Sub


Once done, try to run your project by pressing the F5 key, and select the radiobutton in accordance with the criteria that you will come up, fed the criteria and then click Search button.
Click the following link to download the example source Sql Select stament in vb.net.
Previous
Next Post »