简体   繁体   English

从C#Winforms在SQL数据库中仅保存日期,而不保存时间

[英]Only Date is saved and not time in sql Database from C# winforms

I'm having an issue with saving the time into my SQL database. 我在将时间节省到SQL数据库时遇到问题。

The system should save time and date to the database when the user scans into the building. 当用户扫描建筑物时,系统应将时间和日期保存到数据库中。 At the current moment it saves date but time is saved as 00:00. 当前,它保存日期,但时间保存为00:00。

I had problems converting the date to SQL during the save process, so maybe i've made a mistake in the process. 我在保存过程中将日期转换为SQL时遇到问题,所以也许我在过程中犯了一个错误。 I've looked at it for ages and cant see why it wouldnt save the time as well. 我已经看了很久了,看不到为什么它也不能节省时间。

Any ideas? 有任何想法吗? here is the code in question 这是有问题的代码

  var currentdate = DateTime.Now;
                    var TimeAccess = currentdate.Date.ToString("yyyy-MM-dd HH:mm:ss");
                    SqlCommand com = new SqlCommand();
                    com.Connection = new SqlConnection(Properties.Settings.Default.BioEngineering);
                    com.Connection.Open();

                    com.CommandText = "INSERT INTO AccessHistory (DoorID,UserID,TimeAccess,Status) VALUES (@DoorID,@UserID,@TimeAccess,@Status)";

                    com.Parameters.Add("@DoorID", SqlDbType.Int).Value = DoorNumber;
                    com.Parameters.Add("@UserID", SqlDbType.Int).Value = cboUserID.Text;
                    com.Parameters.Add("@TimeAccess", SqlDbType.DateTime).Value = TimeAccess;
                    com.Parameters.Add("@Status", SqlDbType.VarChar).Value = "Successful";

Thank you 谢谢

如果您的数据库中有datedatetime类型的列,则不应更改其数据类型并将其转换为string ,而应将其直接传递给数据库:

com.Parameters.Add("@TimeAccess", SqlDbType.DateTime).Value = currentDate;

也许用DateTime代替第二行中的Date

var TimeAccess = currentdate.DateTime.ToString("yyyy-MM-dd HH:mm:ss");

You can directly pass DateTime.Now for command parameters and make sure database column is DateTime . 您可以直接传递DateTime.Now作为命令参数,并确保数据库DateTime That's It. 而已。

 com.Parameters.Add("@TimeAccess", SqlDbType.DateTime).Value = DateTime.Now;

You can remove below from your code as It is not required at all. 您可以从代码中删除以下内容,因为完全不需要。

  var currentdate = DateTime.Now;
  var TimeAccess = currentdate.Date.ToString("yyyy-MM-dd HH:mm:ss");
               SqlCommand com = new SqlCommand();
                com.Connection = new SqlConnection(Properties.Settings.Default.BioEngineering);
                com.Connection.Open();

                com.CommandText = "INSERT INTO AccessHistory (DoorID,UserID,TimeAccess,Status) VALUES (@DoorID,@UserID,getdate(),@Status)";

                com.Parameters.Add("@DoorID", SqlDbType.Int).Value = DoorNumber;
                com.Parameters.Add("@UserID", SqlDbType.Int).Value = cboUserID.Text;
               com.Parameters.Add("@Status", SqlDbType.VarChar).Value = "Successful";

Use Getdate() 使用Getdate()

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

相关问题 如何在C#中的控制台应用程序中将SQL Server 2008数据库中的日期(仅)与系统日期(仅)进行比较 - How to compare date(only) from SQL Server 2008 database to the system date(only) in console application in C# Visual C#/ WinForms日期和时间 - Visual C#/WinForms Date and Time 从保存在SQL Server数据库中的二进制文件中检索C#对象 - Retrieve C# object from binary that saved in SQL Server database 在C#winforms中-如何通过日期时间选择器搜索数据库? - In C# winforms - How do you search database by date time picker? 仅在数据库中存储日期而不是时间部分 C# - store only date in database not time portion C# 如何在运行时以编程方式从本地数据库获取连接字符串? c# winforms - how to programmatically get connection string from local database at run time? c# winforms 在C#WinForms中检索上次保存的值 - Retrieving last saved value in C# WinForms 从C#中的数据库比较2个不同的日期/时间 - Compare 2 different date/time from database in C# Winforms:如何使用C#将图片上传到SQL Server数据库 - Winforms: How to upload a picture into SQL Server database using C# 使用C#WinForms将文本框值存储到SQL数据库中 - Storing textbox values into an SQL database using c# winforms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM