Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SrtQuery As String
SrtQuery = "SELECT code,name,end_life FROM Depreciation "
Dim ws As New CheckSv
Ds = ws.ReadDb(SrtQuery, "ShowID")
Try
If Ds.Tables("ShowID").Rows.Count <> 0 Then
With DtgListAll
.ReadOnly = True
.DataSource = Ds.Tables("ShowID")
End With
Button1.Enabled = True
Button2.Enabled = True
End If
formatdatagridwithdata1()
Catch ex As Exception
End Try
End Sub
Dim SelectDate As DateTime = DateTimePicker1.Value
Dim strConn As String = "?????????????"
Dim objConn As New SqlConnection(strConn)
Dim srtQuery As String = "SELECT [code], [name], [end_life] FROM [Depreciation] WHERE [end_life] BETWEEN @start AND @end"
Dim objCmd As New SqlCommand(srtQuery, objConn)
objCmd.Parameters.AddWithValue("@start", StartDate(SelectDate))
objCmd.Parameters.AddWithValue("@end", EndDate(SelectDate))
Dim Dt As New DataTable()
Dim objAdapter As New SqlDataAdapter(objCmd)
objAdapter.Fill(Dt)
' เอา Dt ไปใช้งาน
Code (VB.NET)
Public Function StartDate(ByVal theDate As DateTime) As DateTime
Dim theYear As Integer = theDate.Year
Dim theMonth As Integer = theDate.Month
Return DateTime.Parse(String.Format("1/{0}/{1}"), theMonth.ToString(), theYear.ToString())
End Function
Code (VB.NET)
Public Function EndDate(ByVal theDate As DateTime) As DateTime
Dim amountDay As Integer = New Integer() { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
Dim theYear As Integer = theDate.Year
Dim theMonth As Integer = theDate.Month
Dim theDay As Integer = amountDay(theMonth - 1)
If theMonth = 2 And IsLeapYear(theYear) Then
theDay = 29
End If
Return DateTime.Parse(String.Format("{0}/{1}/{2}"), theDay.ToString(), theMonth.ToString(), theYear.ToString())
End Function
Code (VB.NET)
Public Function IsLeapYear(ByVal year As Integer) As Boolean
If year Mod 4 <> 0 Then
Return False
End If
If year Mod 100 = 0 Then
If year Mod 400 = 0 Then
Return True
Else
Return False
End If
End If
Return True
End Function