Private Sub TestCountDataTable()
Dim dtEmployee As New Data.DataTable
dtEmployee.Columns.Add("EmployeeID", GetType(String))
dtEmployee.Columns.Add("HireDate", GetType(Date))
Dim strCommand As String = "SELECT EmployeeID, HireDate FROM tblEmployee WHERE HireDate IS NOT NULL"
Dim strConnection As String = "Data Source=tg2000;Initial Catalog=ADMIN;Integrated Security=True"
Using sqlConnection As New SqlClient.SqlConnection(strConnection)
sqlConnection.Open()
Using sqlCommand As New SqlClient.SqlCommand(strCommand, sqlConnection)
Using sqlDataReader = sqlCommand.ExecuteReader()
dtEmployee.Load(sqlDataReader)
End Using
End Using
End Using
' USE DataView.RowFilter
Dim dv = dtEmployee.DefaultView
dv.RowFilter = "HireDate>='2010-01-01' and HireDate<='2010-12-31'"
Dim countFromDataView = dv.Count
Debug.Print(String.Format("Count from DataView:{0:#,##0.00}", countFromDataView))
' USE LINQ TO DATASET
Dim countFromLinqToDataSet = dtEmployee.AsEnumerable.Where(Function(w) w.Field(Of Date)("HireDate").Year.Equals(2010)).Count
Debug.Print(String.Format("Count from Linq To DataSet:{0:#,##0.00}", countFromLinqToDataSet))
' USE Datatable.Select
Dim dr() = dtEmployee.Select("HireDate>='2010-01-01' and HireDate<='2010-12-31'")
Dim countFromSelect = dr.Length
Debug.Print(String.Format("Count from Dataset.Select:{0:#,##0.00}", countFromSelect))
End Sub
ถ้าใครนึกวิธีอื่นออกที่น่าสนใจ ก็ช่วยเพิ่มให้ด้วยครับ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น