从MS Access数据库检索数据使用时间戳,即在用户表格上的开始日期和结束日期
问题描述:
我有一个关于使用时间戳从用户表单上检索数据的问题,即开始和结束日期。请让我知道是否以及如何做到这一点。我正在使用Microsoft Visual Studio 2015.网站用户界面将在VB中使用Asp.net。虽然我已经试过路过我不理解为什么它不能在网格视图从MS Access数据库检索数据使用时间戳,即在用户表格上的开始日期和结束日期
ASPX.VB代码反映
Partial Class _Default
Inherits System.Web.UI.Page
Public dad As OleDbDataAdapter
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Protected Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Protected Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim connectionString As [String] = "Provider=Microsoft.ACE.OLEDB.12.0;Data" + " Source=App_Data/VQT_GL Testing.mdb"
Dim ds As New DataSet()
Dim conn As New OleDbConnection(connectionString)
conn.Open()
Dim cmd As New OleDbCommand("SELECT AVG(POLQA_Score) AS MEAN FROM VQTPOLQA WHERE VQT_Timestamp BETWEEN ? AND ?", conn)
cmd.Parameters.AddWithValue("@StartDate", (TextBox3.Text))
cmd.Parameters.AddWithValue("@EndDate", (TextBox2.Text))
GridView1.DataSource = ds
End Sub
End Class
ASPX CODE
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>MOS FORM HOME</title>
<script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"type="text/javascript"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"/>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript" ></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-timepicker-addon.js"></script>
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<link href="Styles/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
$(document).ready(function() {
$("#<%=TextBox3.ClientID %>").dynDateTime({
showsTime: true,
ifFormat: "%m/%d/%Y %H:%M:%S",
daFormat: "%l;%M %p, %e %m, %Y",
align: "BR",
electric: false,
singleClick: false,
displayArea: ".siblings('.dtcDisplayArea')",
button: ".next()"
});
$("#<%=TextBox2.ClientID %>").dynDateTime({
showsTime: true,
ifFormat: "%m/%d/%Y %H:%M:%S",
daFormat: "%l;%M %p, %e %m, %Y",
align: "BR",
electric: false,
singleClick: false,
displayArea: ".siblings('.dtcDisplayArea')",
button: ".next()"
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 74px">
MOS FORM HOME PAGE</div>
<p>
START DATE
<asp:TextBox ID="TextBox3" runat="server" TextMode="DateTime" Width="181px" BorderColor="Black" BorderStyle="Solid"></asp:TextBox><img src="calender.png" />
</p>
<p>
END DATE <asp:TextBox ID="TextBox2" runat="server" TextMode="DateTime" Width="188px" BorderColor="Black" BorderStyle="Solid"></asp:TextBox><img src="calender.png" />
</p>
<p>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="btnSave_Click"/>
</p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataSourceID="SqlDataSource2" GridLines="Horizontal" Height="146px" Width="53px">
<Columns>
<asp:BoundField DataField="MEAN" HeaderText="MEAN" ReadOnly="True" SortExpression="MEAN" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:VQT_GL TestingConnectionString %>" ProviderName="<%$ ConnectionStrings:VQT_GL TestingConnectionString.ProviderName %>" SelectCommand="SELECT AVG(POLQA_Score) AS MEAN FROM VQTPOLQA WHERE VQT_Timestamp BETWEEN ? AND ?">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox3" DefaultValue="" Name="?" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" DefaultValue="" Name="?" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:AccessDataSource runat="server" DataFile="~/App_Data/VQT_GL Testing.mdb" SelectCommand="SELECT AVG(POLQA_Score) AS MEANPOLQA FROM VQTPOLQA WHERE (VQT_Timestamp BETWEEN ? AND ?)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox3" DefaultValue="" Name="?" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" Name="?" PropertyName="Text" />
</SelectParameters>
</asp:AccessDataSource>
</form>
</body>
</html>
答
参数乍一看,你的问题似乎与你的OleDbCommand参数有关。您正在使用cmd.Parameters.AddWithValue。这是我倾向于避免的,因为参数的数据类型是从传递的值中推断出来的。在这种情况下,你正在传递文本。这意味着该参数被视为字符串参数而不是DateTime。这几乎肯定会失败! 相反,您应该自己设置参数的类型。要做到这一点使用类似:
Dim param as OleDbParameter
param = cmd.CreateParameter
param.OleDbType = OleDbType.Date
param.Value = DateTime.Parse(TextBox1.Text)
param.ParameterName = "@myDate"
cmd.Parameters.Add(param)
注意访问OleDbType需要是日期即使该字段是DateTime。
这可能是我在上面的语法错误 - 这是很长一段时间,因为我用VB,我不记得你什么时候需要圆括号,当你不需要,但它应该是像这(我可以完全在C#中给你)。我相信你可以纠正我的VB。
其次,你并没有真正执行你的OleDbCommand!这里有不同的可能性。您可以使用OleDbDataAdapter或OleDbDataReader。我个人更喜欢使用OleDbDataReader,然后从结果中构建自己的DataTable以用作DataSource,但您可能会发现适配器更容易。请谷歌了解如何做到这一点的例子!它应该是这样的:
Dim adapter as New OleDbDataAdapter(cmd)
adapter.Fill(ds)
再次我的VB不是最新的!
希望这会有所帮助
非常感谢您的更新,现在解析很好,我也能够获得正确的记录值。 – Fgst
很高兴有帮助。希望我的vb不太可怕。自从我自己写了任何vb以来,真的很长。 –
你的VB绝对完美! – Fgst