你要存儲一個int類型將存儲年份部分進入數據庫的一年。
或者,如果你想離開DateTime類型到數據庫中,你可以創建一個模型元數據類MVC,然後創建一個新的詮釋財產「獲得年度」,請參閱以下內容:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace YourWebApp.Models
{
[MetadataType(typeof(YourClassMetaData))]
public partial class YourClass
{
[DisplayName("Year Obtained")]
public int YYObtained {
get
{
return this.reDateFrom.Year;
}
}
}
public class YourClassMetaData
{
[DataType(DataType.Date)]
public DateTime reDateFrom { get; set; }
}
}
的http://計算器.com/questions/696506/sql-datatype-how-to-store-a-year回答你的問題:) – Roman
'DisplayFormat'屬性只會影響使用諸如'Html.DisplayFor'等方法的顯示格式,而不會影響數據庫格式。 – DavidG
如何設置數據庫格式的年份? – Ejazkhan