请任何人帮助我,我只是制作一个微型金融类型的软件,有一个问题我想每周显示从访问数据库到vb.net数据网格视图的数据,但不能正常工作我的代码,
我插入EntryDate Savings Entry Lable Date(lblSavingsEntryDate.Text = Date.Now.ToString(" dd / MM / yyyy"))
这是我的代码
Private Sub btnBalanceWeekly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBalanceWeekly.Click
Dim Sunday = DateTime.Now.AddDays((Today.DayOfWeek - DayOfWeek.Sunday) * -1).ToString("dd/MM/yyyy")
Dim todate = DateTime.Now.AddDays(0).ToString("dd/MM/yyyy")
Try
Dim sqlstr1 As String
sqlstr1 = "SELECT * FROM Receivedtbl WHERE EntryDate BETWEEN '" + Sunday + "' And '" + todate + "'"
Dim da As New OleDbDataAdapter(sqlstr1, conn2)
Dim dt As New DataTable("Receivedtbl")
da.Fill(dt)
dgvBalanceSavings.DataSource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn2.Close()
Me.BalanceTotalSeavings()
Me.BalanceGrpReceived()
Me.BalanceCusReceived()
End Try
End Sub
请帮助...如何在每周显示数据。
答案 0 :(得分:1)
使用Parameters
(DataAdapter
将以正确的方式转换date
,datetime
格式在sql中)而不是将date
转换为更安全更安全的方式string
,特别是因为不同的日期格式和避免sql注入。
Bellow就是使用Parameters
,例如源格式(在本例中为date
数据类型)的示例:
Private Sub btnBalanceWeekly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBalanceWeekly.Click
Dim Sunday = DateTime.Now.AddDays((Today.DayOfWeek - DayOfWeek.Sunday) * -1)
Dim todate = DateTime.Now.AddDays(0)
Try
Dim sqlstr1 As String
sqlstr1 = "SELECT * FROM Receivedtbl WHERE EntryDate BETWEEN @sunday AND @todate;"
Dim da As New OleDbDataAdapter(sqlstr1, conn2)
da.SelectCommand.Parameters.AddWithValue("@sunday", Sunday)
da.SelectCommand.Parameters.AddWithValue("@todate", todate)
Dim dt As New DataTable("Receivedtbl")
da.Fill(dt)
dgvBalanceSavings.DataSource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn2.Close()
Me.BalanceTotalSeavings()
Me.BalanceGrpReceived()
Me.BalanceCusReceived()
End Try
End Sub
通过这种方式,您无需担心date
格式转换。
答案 1 :(得分:0)
您尝试将日期(字符串日期)比较为“dd / MM / yyyy”格式,并且您遇到了一个大问题,因为您的查询在运行时将是这样的:
在哪里EntryDate BETWEEN '11 / 201/2018'和'12 / 02/2018'
结果:大量字符串在这些日期之间
例如:'11 / 02/2018','11/03/201','11/04/201',甚至是'11 / 02/2019'的另一年
在字符串比较中,早期字符总是首先进行比较,它们在比较系统中更为重要(在这种情况下,您的4个字符的年份优先级低于月份甚至白天)
解决方案:
使用此格式保存日期:
的ToString( “YYYY / MM / DD”)
所以你的代码应该是这样的:
Private Sub btnBalanceWeekly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBalanceWeekly.Click
Dim Sunday = DateTime.Now.AddDays((Today.DayOfWeek - DayOfWeek.Sunday) * -1).ToString("yyyy/MM/dd")
Dim todate = DateTime.Now.AddDays(0).ToString("yyyy/MM/dd")
Try
Dim sqlstr1 As String
sqlstr1 = "SELECT * FROM Receivedtbl WHERE EntryDate BETWEEN '" + Sunday + "' And '" + todate + "'"
Dim da As New OleDbDataAdapter(sqlstr1, conn2)
Dim dt As New DataTable("Receivedtbl")
da.Fill(dt)
dgvBalanceSavings.DataSource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn2.Close()
Me.BalanceTotalSeavings()
Me.BalanceGrpReceived()
Me.BalanceCusReceived()
End Try
End Sub
但请记住,您也需要使用此格式保存日期字符串(已保存在数据库中的旧日期)
答案 2 :(得分:0)
首先永远打开Option Strict。编译时错误 - 好,你可以修复它们。运行时错误很糟糕,你可能无法捕获它们。 向表单和适当的标签添加两个DateTimePicker控件。如果默认值不适合您,可以设置各种属性。 以下代码显示如何使用SQL字符串的参数。这不仅可以节省您编写字符串的麻烦,还可以保护您的数据库免受恶意输入。
Private Sub GetSavings()
Dim da As New OleDbDataAdapter
Dim strSQL As String = "SELECT * FROM Receivedtbl WHERE EntryDate BETWEEN @FromDate And @ToDate;"
Dim FromDate As Date = DateTimePicker1.Value.Date
Dim ToDate As Date = DateTimePicker2.Value.Date
If FromDate >= ToDate Then
MessageBox.Show("From date must be earlier than To date.")
Exit Sub
End If
Dim cmd As New OleDbCommand With {
.Connection = conn2,
.CommandType = CommandType.Text,
.CommandText = strSQL}
'Access cares not about the parameter names but about there order
cmd.Parameters.Add("@FromDate", OleDbType.Date).Value = FromDate
cmd.Parameters.Add("@ToDate", OleDbType.Date).Value = ToDate
da.SelectCommand = cmd
'now continue with your .Fill code
End Sub