 |
|
แสดงข้อมูลเฉพาะวันที่ ที่ตรงกับ DateTimePicker ทำยังไงค่ะ |
|
 |
|
|
 |
 |
|
datetime
- .month
- .year
sql statement
- between
|
 |
 |
 |
 |
Date :
2010-08-23 09:08:45 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอตัวอย่าง code ให้พอเข้าใจหน่อยได้ไหมค่ะ
ถ้าเลือก DateTimePicker1 แล้วกดปุ่มค้นหา ให้แสดง ฟิวแค่ 3 ฟิว ที่เดือนกะปีตรงกะใน DateTimePicker1ค่ะ
|
 |
 |
 |
 |
Date :
2010-08-23 14:16:24 |
By :
pumka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
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
อันนี้มันจะแสดงทั้งหมดค่ะ แต่อยากให้แสดงเฉพาะวันที่ ที่เดือนกะปีตรงกับ DateTimePicker ไม่รู้ว่า
จะต้องเอาค่าในDateTimePicker เปรียบ เทียบกะฐานข้อมูลยังไง สงสารคนฉลาดน้อยด้วยนะค่ะ
|
 |
 |
 |
 |
Date :
2010-08-24 14:33:35 |
By :
pumka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หูย พูดซะน่าสงสารจัง พอดีไม่เชี่ยว sql command ซะด้วย เอาวิธีของคนบ้าไปลองดูแล้วกันนะ
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2010-08-24 15:35:10 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหอะๆ ใช้vb2005ก็งงนิดหน่อย แต่ก็ขอบคุณมากๆนะค่ะ
|
 |
 |
 |
 |
Date :
2010-08-25 01:51:32 |
By :
pumka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|