简体   繁体   English

如何使用C#在Ms Access中插入当前日期/时间

[英]how to insert current date/time in Ms Access using c#

I have tried almost everything to insert the current date and time in to MS Access database using c# and nothing seems to work. 我已经尝试了几乎所有使用c#将当前日期和时间插入MS Access数据库的方法,但似乎没有任何效果。 The table structure looks like 表结构看起来像

ID(Primary Key) Est_ID(Number)  saveName(Text) userName(Text) timestamp(Date/Time)

The code looks like 代码看起来像

OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();

DateTime today = DateTime.Today;
DateTime time= DateTime.Now;
string name = Request.Form["saveName"];

string my_querry = "INSERT INTO TABLENAME (userName, Est_id,saveName,timestamp) VALUES(@userName,@Est_id,@saveName,@timestamp)";

OleDbCommand cmd = new OleDbCommand(my_querry, conn);

cmd.Parameters.AddWithValue("@userName", userName);
cmd.Parameters.AddWithValue("@Est_id", est_index);
cmd.Parameters.AddWithValue("@saveName", name);
cmd.Parameters.AddWithValue("@timestamp",time);

cmd.ExecuteNonQuery();

I have tried formatting. 我尝试过格式化。 I added # but nothing seems to work. 我添加了#,但似乎没有任何效果。 This is really frustrating since I have spent more than a day now getting this to work. 因为我花了超过一天的时间才能使它工作,所以这实在令人沮丧。 What am I doing wrong? 我究竟做错了什么?

OleDB/Access requires you to explicitly set the data type for date/time values. OleDB / Access要求您显式设置日期/时间值的数据类型。

var param = cmd.Parameters.AddWithValue("@timestamp",time);
param.OleDbType = OleDbType.Date;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();

string name = Request.Form["saveName"];

string my_querry = "INSERT INTO TABLENAME (userName, Est_id,saveName,timestamp) VALUES(@userName,@Est_id,@saveName,Now())";

OleDbCommand cmd = new OleDbCommand(my_querry, conn);

cmd.Parameters.AddWithValue("@userName", userName);
cmd.Parameters.AddWithValue("@Est_id", est_index);
cmd.Parameters.AddWithValue("@saveName", name);


cmd.ExecuteNonQuery();

According from this link https://support.office.com/en-us/article/Format-the-date-and-time-field-in-Access-47fbbdc1-52fa-416a-b8d5-ba24d881b698 根据此链接https://support.office.com/en-us/article/Format-the-date-and-time-field-in-Access-47fbbdc1-52fa-416a-b8d5-ba24d881b698

You may need to check on your column setting in database that It's already set Format of timestamp column to Genaral Date to match with DateTime.Now in C#. 您可能需要检查数据库中的列设置,它是否已将timestamp列的Format设置为Genaral Date以与C#中的DateTime.Now匹配。

Also, You need to check that output of System.DateTime and your database time format is 12Hours system or 24Hours system. 另外,您需要检查System.DateTime的输出和数据库时间格式是12Hours还是24Hours。

Hope this information can help you. 希望这些信息能对您有所帮助。

If you are using Tortuga Chain: 如果您使用的是Tortuga链条:

static AccessDataSource s_DataSource = new AccessDataSource(connectionString); //one per application

DateTime today = DateTime.Today;
DateTime time= DateTime.Now;
string name = Request.Form["saveName"];

s_DataSource.Insert("TABLENAME", new {@username = userName, @Est_id = est_index,@saveName = name,@timestamp = time}).Execute();

I know this post is a little old but it could help someone. 我知道这篇文章有些旧,但可以对某人有所帮助。 Since this morning I am confronted with this problem. 从今天早上开始,我面临这个问题。 And I finally found the solution. 我终于找到了解决方案。

The value that DateTime.Now sends to the database is in the form {dd / mm / yyyy hh: mm: ss} and what happens, I think, is that access does not recognize { } . DateTime.Now发送到数据库的值的格式为{dd / mm / yyyy hh: mm: ss} ,我认为访问不会识别{ }

So to solve the problem just convert the date to string before assigning it to the parameters: 因此,要解决该问题,只需将日期转换为字符串,然后再将其分配给参数即可:

cmd.Parameters.AddWithvalue("@timestamp", time.ToString());

Or 要么

cmd.Parameters.AddWithvalue("@timestamp", oleDbType.Date).Value=time.ToString());

That work very well for me 那对我来说很好

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

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