仅存储日期但不存储时间到数据库ASP.NET C# [英] Store only date but not time into database ASP.NET C#

查看:74
本文介绍了仅存储日期但不存储时间到数据库ASP.NET C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将唯一的日期存储到数据库中而不是时间。

我在表中有日期数据类型,但问题是,它存储日期和时间都像

02/25/15 00:00:00。但我只想要日期,而不是时间。

我正在使用带有TextMode =date的TextBox来插入日期。



< b>我尝试了什么:



 受保护  void  Button1_Click( object  sender,EventArgs e)
{
SqlCommand command = < span class =code-keyword> new SqlCommand( SPinsertitem,con) ;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add( @ rdatep,SqlDbType.Date).Value = txt_rdate.Text;
try
{

command.ExecuteNonQuery();
Label1.Text =( 成功添加);
}
catch (SqlException ex)
{
Label1.Text =( SQL错误 + ex.Message.ToString());

}

解决方案

在表格中,DataType列应该是Date而不是DateTime。这是一个例子



  DECLARE   @ temp   as   TABLE 
DateColumn1 Date
DateColumn2 DateTime


INSERT INTO @ temp
SELECT ' 1/1 2017'' 1/1/2017'

SELECT * FROM @ temp





输出

 DateColumn1 DateColumn2 
2017-01-01 2017-01-01 00 :00:00.000


无论或不可以,这取决于您的数据库引擎。您使用的是哪个版本的SQL Server?



数据库中的列类型是什么?如果它是datetime(常见),这是你真正不需要担心的事情,除非磁盘空间是个问题。将时间显示为日期的一部分是显示与否的UI选择,而不是数据库中的实际选择。



如果您可以将列类型更改为日期(仅在SQL Server 2008及更高版本中),那么您应该这样做。请记住,代码中没有等效类型。您仍然会获得一个DateTime对象,其中包含数据库中的日期和设置为午夜的时间。


Hi everyone,
I want to store the only date into the database but not time.
I have "date" DataType in Table but problem is, it's storing Date and Time both like
02/25/15 00:00:00. But I want only the date, not time.
And I am using TextBox with TextMode="date" to insert date.

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand command = new SqlCommand("SPinsertitem", con);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@rdatep", SqlDbType.Date).Value = txt_rdate.Text;
        try
        {
            
            command.ExecuteNonQuery();
            Label1.Text = ("Successfully ADDED");
        }
        catch (SqlException ex)
        {
            Label1.Text = ("SQL Error" + ex.Message.ToString());
            
        }

解决方案

In the table, the column DataType should be Date and not DateTime. Here is an example

DECLARE @temp as TABLE (
	DateColumn1 Date,
	DateColumn2 DateTime
)

INSERT INTO @temp
	SELECT '1/1/2017','1/1/2017'

SELECT * FROM @temp



Output:

DateColumn1	DateColumn2
2017-01-01	2017-01-01 00:00:00.000


Whether or not you can do this is dependent on your database engine. What version of SQL Server are you using?

What's the column type in the database? If it's datetime (common), this is something you really don't need to worry about, unless disk space is an issue. Showing the time as part of the date is a UI choice to show it or not, not really a choice in the database.

If you can change the column type to Date (only in SQL Server 2008 and above), then you should do that instead. Keep in mind, there is no equivilent type in your code. You will still get a DateTime object with the date from the database and the time set at midnight.


这篇关于仅存储日期但不存储时间到数据库ASP.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆