C# 仅显示日期的月份名称。立即

C# 仅显示日期的月份名称。立即,c#,asp.net,C#,Asp.net,我有一个asp.net应用程序,只要点击一个按钮,它就会写入我的数据库。我的问题是,对于日期列,我只想显示月份名称,而不想显示其他内容 我尝试了下面的方法,但我总是收到一条消息,说“字符串未被识别为有效的日期时间。” 第一个解决方案 SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text; SqlPastPlaces.InsertParameters["Month"].D

我有一个
asp.net
应用程序,只要点击一个按钮,它就会写入我的数据库。我的问题是,对于日期列,我只想显示月份名称,而不想显示其他内容

我尝试了下面的方法,但我总是收到一条消息,说“字符串未被识别为有效的日期时间。”

第一个解决方案

SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text;
SqlPastPlaces.InsertParameters["Month"].DefaultValue = DateTime.Now.ToString("MMMM");

SqlPastPlaces.Insert();
var CurrentMonth = String.Format("{0:MMMM}", DateTime.Now).ToString();
SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text;
SqlPastPlaces.InsertParameters["Month"].DefaultValue = CurrentMonth;

SqlPastPlaces.Insert();
第二种解决方案

SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text;
SqlPastPlaces.InsertParameters["Month"].DefaultValue = DateTime.Now.ToString("MMMM");

SqlPastPlaces.Insert();
var CurrentMonth = String.Format("{0:MMMM}", DateTime.Now).ToString();
SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text;
SqlPastPlaces.InsertParameters["Month"].DefaultValue = CurrentMonth;

SqlPastPlaces.Insert();
第二种解决方案,当我调试时确实给我“十月”,但在插入时不写入数据库

这两种解决方案都死在
sqlpassplaces.Insert()
上。my db中的列设置为
varchar


这也在我的代码隐藏中。

如果要插入数据的列的类型为DateTime,则需要插入有效的日期时间。一个字符串是不够的

SqlPastPlaces.InsertParameters["Month"].DefaultValue = DateTime.Now;
我不会使用varchar或integer列来存储日期部分,因为一旦您需要将其更改为显示日期和月份或年份和月份,它将导致大量额外的工作。
存储日期并只查询/显示您需要的数据。

找到了cuprite,它在我的视图中。以下行导致了问题

<asp:Parameter DbType="Date" Name="Month" />

通过将其更改为以下内容,它现在可以正常工作

<asp:Parameter DbType="String" Name="Month" />


我完全忘了更改此位。

再次检查该月是否确实是varchar-似乎是datetime;另一个要检查的地方是SqlPastPlaces。InsertParameters[“Month”]-检查它是否定义为varchar参数。验证在数据集和参数声明中
Month
未声明为
DateTime
,即使SQL列为
varchar