简体   繁体   English

如何使用C#和SQL将日期添加到列类型为“日期/类型”的MS Access数据库中?

[英]How do I add a date using C# & SQL to an MS Access database with column type 'Date/Type'?

I have tried using a DateTimePicker with format dd/MM/yyyy to insert a date into a Date/Time column in my MS Access database using the insert SQL statement. 我尝试使用DateTimePicker格式为dd/MM/yyyyDateTimePicker使用插入SQL语句在MS Access数据库的Date/Time列中插入日期。

Here is what I have done: 这是我所做的:

connection.Open();

OleDbCommand command = new OleDbCommand();
command.Connection = connection; // Connecting the command to the connection.
command.CommandText = "insert into [TestEntry] (TestTableID, StudentID, DateOfTest, [DeadLine]) values(" + int.Parse(txtTestName.Text) + ", " + int.Parse(studentID) + ", #" + this.dateTimeStartD.Text + "# , #" + this.dateTimeEndD.Text + "#)";

command.ExecuteNonQuery();
connection.Close();

I keep getting the error 我不断收到错误

Input string was not in correct format 输入的字符串格式不正确

The Date/Time columns in my Access table are formatted as 'Short date' eg. 我的Access表中的“日期/时间”列的格式为“短日期”,例如。 12/11/2015. 2015年12月11日。

Any help will be appreciated. 任何帮助将不胜感激。

I do not think its coming from database level. 我不认为它来自数据库级别。 This exception is coming when you try to case value which in different format. 当您尝试以不同格式区分值时,将出现此异常。 Please check you first argument for the insert statement. 请检查您的第一个参数是否为insert语句。

int.Parse(txtTestName.Text)

You are trying to insert integer value to "TestTableID" field by converting text box value to integer. 您正在尝试通过将文本框值转换为整数将整数值插入“ TestTableID”字段。 Please check your text box input is number or characters. 请检查您的文本框输入的是数字还是字符。 It should be only numbers. 应该只是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM