Dim timeFormat As String = "yyyy-MM-dd HH:mm:ss"
Dim fromdate = SurrenderListForm.FromDatePicker.Value
Dim todate = SurrenderListForm.ToDatePicker.Value
MsgBox(fromdate.ToString(timeFormat))
MsgBox(todate.ToString(timeFormat))
SqlCon.Open()
SqlCommand = New SqlCommand("SELECT * FROM profile_tbl Where date_surrendered >= '" & fromdate.ToString(timeFormat) & "' AND
date_surrendered < '" & todate.ToString(timeFormat) & "'", SqlCon)
SqlAdapter = New SqlDataAdapter(SqlCommand)
SqlDataset = New DataSet
SqlAdapter.Fill(SqlDataset)
SqlCon.Close()
答案 0 :(得分:0)
只需尝试将您的查询修改为类似的内容
SELECT * FROM profile_tbl Where date_surrendered
BETWEEN
'" & fromdate.ToString(timeFormat) & "'
AND
'" & todate.ToString(timeFormat) & "'
这里有一些其他帮助@ W3 Schools
答案 1 :(得分:0)
问题可能是日期被视为字符串。这通常会导致代码无法正常工作。另外,在SQL中将参数作为字符串放入是错误的方法:需要将值作为SQL参数添加。幸运的是,很容易以正确的方式做到这一点:
Dim fromdate = SurrenderListForm.FromDatePicker.Value
Dim todate = SurrenderListForm.ToDatePicker.Value
fromdate = fromdate - TimeSpan.FromMilliseconds(fromdate.Millisecond)
todate = todate - TimeSpan.FromMilliseconds(todate.Millisecond)
Dim connStr = "connection string goes here"
Dim sql = "SELECT * FROM profile_tbl Where date_surrendered >= @FromDate AND date_surrendered < @ToDate"
Using sqlConn As New SqlConnection(connStr)
Using sqlCmd As New SqlCommand(sql, sqlConn)
sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@FromDate", .SqlDbType = SqlDbType.DateTime, .Value = fromdate})
sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@ToDate", .SqlDbType = SqlDbType.DateTime, .Value = todate})
Dim SqlAdapter = New SqlDataAdapter(sqlCmd)
SqlDataset = New DataSet
SqlAdapter.Fill(SqlDataset)
End Using
End Using
我从timeFormat
变量中假设您不想涉及任何毫秒。
Fill
方法将自行打开和关闭连接。
Using
的构造可确保调用Dispose,以便正确释放非托管资源。