【发布时间】:2013-04-10 15:30:28
【问题描述】:
我正在使用日历扩展器在 Visual Studio 2010 内部的 ASP.NET 中扩展一个文本框。我正在尝试将事件的日期与其他信息一起插入到数据库中。我在尝试插入数据库时收到“标准表达式中的数据类型不匹配”错误。
我尝试使用 DateTime.ParseExact 将字符串日期转换为访问日期/时间,但仍然没有成功。
这是我的代码:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString) Dim SqlString As String = "Insert into Events(EventTitle,EventDescription,EventDate,EventCategory) Values (@f1,@f2,@f3,@f4)" Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn) cmd.CommandType = CommandType.Text cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text) cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text) cmd.Parameters.AddWithValue("@f3", DateTime.ParseExact(tb_eventdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)) cmd.Parameters.AddWithValue("@f4", dd_eventcategory.SelectedValue) oleDbConn.Open() cmd.ExecuteNonQuery() System.Threading.Thread.Sleep("2000") Response.Redirect("~/calendar.aspx") End Sub
这是我的 ASP.NET 代码(请注意,我还将 CalendarExtender 插入文本框中的日期格式化为“dd/MM/yyyy”):
<asp:TextBox ID="tb_eventdate" runat="server" ToolTip="Enter a date"></asp:TextBox> <ajaxToolkit:CalendarExtender ID="tb_eventdate_CalendarExtender" Format="dd/MM/yyyy" runat="server" TargetControlID="tb_eventdate"> </ajaxToolkit:CalendarExtender>
我的 Access 数据库中的字段类型为“日期/时间”。
我不知道为什么会遇到这个问题,因为我设法在另一个函数中从数据库中检索日期并将它们转换为 ToString:
Function GetEventListing(selectedDay As DateTime) As DataTable '--read event listing for the given day from an Access query Dim con As OleDbConnection = GetConnection() Dim cmd As OleDbCommand = New OleDbCommand() cmd.Connection = con cmd.CommandText = String.Format("Select * from EventInfo Where EventDate >= #{0}# And EventDate < #{1}#", _ selectedDay.ToString("dd/MM/yyyy"), _ selectedDay.AddDays(1).ToString("dd/MM/yyyy")) Dim ds As DataSet = New DataSet() Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd) da.Fill(ds) con.Close() Return ds.Tables(0) End Function
我收到错误的原因可能是什么?
【问题讨论】:
-
我认为访问数据库将“-”作为分隔符。但不确定。
-
检查您的区域设置以获取正确的日期格式
-
@George 我在哪里可以找到我的区域设置?
标签: asp.net vb.net ms-access datetime-format