“条件表达式中的数据类型不匹配”错误将日期插入Access数据库中的日期/时间字段

时间:2013-04-10 15:30:29

标签: asp.net vb.net ms-access datetime-format

我正在使用Calendar扩展程序在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

我收到的错误可能是什么原因?

1 个答案:

答案 0 :(得分:3)

也许这不是弄乱你的日期。我想也许你得到了错误,因为你添加了DateTime值作为参数(而不是转换为格式为yyyy-mm-ddm/d/yyyy的字符串的日期),但我尝试了跟随C#,它运作良好......

static void Main(string[] args)
{
    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Administrator\Desktop\Database1.accdb;");
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("INSERT INTO Events (EventName, EventDate) VALUES (?, ?)", conn);
    cmd.Parameters.AddWithValue("?", "TestEvent");
    cmd.Parameters.AddWithValue("?", (new DateTime(2013,3,21)));
    cmd.ExecuteNonQuery();
    conn.Close();

    Console.WriteLine("Done.");
}

...所以,如果您的DateTime解析返回有效的DateTime值,那么您的查询应该工作。

如果确实是执行失败的SQL语句,唯一可能的另一个可能是dd_eventcategory.SelectedValue。也许这需要.ToString()'d ......?