无法将日期添加到 Access 数据库

Failed to add Date to Access database

将生日数据添加到 Access 数据库时遇到一些问题。我已经尝试 运行 没有 'birthday' 妨碍的代码,然后它就可以工作了。根据我的研究,'DBDate' 也考虑了毫秒数,我尝试将文本字段中的输入保存为 DateTime 格式,但遗憾的是,这也没有用。

这是我正在使用的代码。

public partial class Records : System.Web.UI.Page
{
    OleDbConnection con;
    OleDbCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Trace.Warn("2310", "Beginning of life Page_Load");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString=@"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source=E:\Dropbox\Temporary\OOD Assignment\Employee Directory\AppData\Employee.accdb";

        String empName;
        String ebirth;
        String job;
        String location;
        String pnumber;
        String email;

        OleDbCommand cmd = new OleDbCommand("INSERT into EmployeeRecords ([ename],[job],[location],[phonenumber],[email],[birth])Values(@empName,@job,@location,@pnumber,@email,@ebirth)");
    cmd.Connection = conn;

        conn.Open();

        if(conn.State == ConnectionState.Open)
        {
            cmd.Parameters.Add("@empName", OleDbType.VarChar).Value = tbEName.Text;
            cmd.Parameters.Add("@ebirth", OleDbType.DBDate).Value = tbBirthDate.Text;
            cmd.Parameters.Add("@job", OleDbType.VarChar).Value = tbJobTitle.Text;
            cmd.Parameters.Add("@location", OleDbType.VarChar).Value = tbOfficeLocation.Text;
            cmd.Parameters.Add("@pnumber", OleDbType.Numeric).Value = tbPNumber.Text;
            cmd.Parameters.Add("@email", OleDbType.VarChar).Value = tbEmail.Text;

            try
            {
                cmd.ExecuteNonQuery();
                Label1.Text = "Data Added";
                conn.Close();
            }
            catch(OleDbException ex)
            {
                Label1.Text = "Exception" + ex;
                conn.Close();
            }
        } else
        {
            Label1.Text = "Connection Failed!";
        }
    }
}

这是我得到的错误:

ExceptionSystem.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at Records.btnSubmit_Click(Object sender, EventArgs e) in e:\Dropbox\Temporary\OOD Assignment\Employee Directory\Records.aspx.cs:line 54

Employee.accdb文件中"birth"的字段类型为Date/Time。

有什么方法可以将文本字段中的用户输入转换为 Access 可以理解的格式吗?任何帮助将非常感激。

DBDate mapped with DateTime 在 CLR 端。

这意味着,您需要先将 tbBirthDate.Text 解析为 DateTime

cmd.Parameters.Add("@ebirth", OleDbType.DBDate).Value = DateTime.Parse(tbBirthDate.Text);

如果您的文本框值不是 CurrentCulture 的标准日期和时间格式,您可以使用 DateTime.ParseExact method 解析它,并使用您的字符串的确切格式。

还可以使用 using statement 自动处理您的连接和命令,而不是手动调用 CloseDispose 方法。

顺便说一下,OleDbCommand 不支持命名参数。实际上,它支持,但只是 不关心。只关心它们的参数值。

OleDbCommand cmd = new OleDbCommand(@"INSERT into EmployeeRecords ([ename],[job],[location],[phonenumber],[email],[birth])
                                      Values(@empName,@job,@location,@pnumber,@email,@ebirth)");
....
cmd.Parameters.Add("@empName", OleDbType.VarChar).Value = tbEName.Text;
cmd.Parameters.Add("@job", OleDbType.VarChar).Value = tbJobTitle.Text;
cmd.Parameters.Add("@location", OleDbType.VarChar).Value = tbOfficeLocation.Text;
cmd.Parameters.Add("@pnumber", OleDbType.Numeric).Value = tbPNumber.Text;
cmd.Parameters.Add("@email", OleDbType.VarChar).Value = tbEmail.Text;
cmd.Parameters.Add("@ebirth", OleDbType.DBDate).Value = DateTime.Parse(tbBirthDate.Text);

您不能将字符串传递给 Date/Time。您需要将日期转换为日期时间:

DateTime value = new DateTime(year, month, day);

其他方法是使用 DateTimePicker 而不是 TextBox。请注意,因为据我所知,OleDB 无法处理毫秒数。