将生日值从日,月,年下拉列表中插入数据库

时间:2017-06-21 09:25:54

标签: c# asp.net

在我的表单中,我有三个单独的生日下拉列表:日,月和年。 在我的数据库中,我有一个类型为date的“生日”列。

如何从特定日期格式的下拉列表中转换这些值以便在数据库中接受?

下拉列表值:

Day       Month         Year

1          Jan          1990
2          Feb          1991
3          Mar          1992

......等等。

我试过这个。它有效,但我知道有更好的方法:

DateTime bday = DateTime.Parse(String.Format("{0}/{1}/{2}", dropDay.SelectedValue, dropMonth.SelectedValue, dropYear.SelectedValue));

2 个答案:

答案 0 :(得分:2)

您应该使用DateTime并按照以下方式启动它:

DateTime birthday = new DateTime(int year, int month, int day);

答案 1 :(得分:0)

首先你必须将你的Month字符串解析为int,然后你应该使用DateTime并像这样初始化它(如AitorFDK所写):

int month = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month
DateTime birthday = new DateTime(int year, int month, int day);
相关问题