如何从数据库中的datetime仅检索日期 [英] how to retrieve only date from datetime from database

查看:150
本文介绍了如何从数据库中的datetime仅检索日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中我有一个出生日期,一个文本框填写出生日期,当一个人填写他的出生日期,如01/01/1990,然后它将被保存到数据库,并且当我检索这个日期表格数据库然后这是01/01/1990 12:00 AM,我只想要它的日期部分。





这是我填写出生日期的表格



form.aspx

i have a form in which i have a field date of birth , and a textbox to fill date of birth , when a person fills his date of birth like 01/01/1990 , then it will be saved into database , and when i retrieve this date form database then this comes 01/01/1990 12:00 AM , i want only date part from it.


This is my form to fillup date of birth

form.aspx

 <tr>
    <td width="30%">
        Date of Birth
        <br />
    </td>
    <td style="width: 1px">
       :<asp:TextBox ID="txtDateofBirth" runat="server" placeholder="dd/mm/yyyy"></asp:TextBox>
    </td>
    <td class="td2">
    </td>
</tr>





这是表单的代码。 aspx.cs



This is code for form.aspx.cs

SqlCommand cmd = new SqlCommand(
"insert into ApplicantForm(NameofApplicant, dateofbirth)"
+ "values( @NameofApplicant, convert(Datetime, @dateofBirth, 103)", con);

cmd.Parameters.AddWithValue("@dateofBirth",txtDateofBirth.Text);







这是打印出生日期的代码

print.aspx




this is code for print of this date of birth
print.aspx

<asp:Label ID="txtDOB" runat="server"  ></asp:Label>





这是代码< b> print.aspx.cs



this is code print.aspx.cs

SqlCommand cmd = new SqlCommand(
"Select * from ApplicantForm 
Where ApplicantId=" + ((Request.QueryString["ApplicantId"])) + "", con);

dr = cmd.ExecuteReader();

txtDOB.Text = dr["dob"].ToString();

推荐答案

修改如下







Modify as below



txtDOB.Text = dr["dob"].ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

< br $>




确保在使用CultureInfo时添加System.Globalization命名空间;




Make sure to add System.Globalization namespace when using CultureInfo;


解决方案No.1 :如果您使用SQl Server 2008及以上数据库U可以使用数据类型

作为日期。



解决方案No.2:U can使用convert(varchar(10),GETDATE(),105)

convert(varchar(10),GETDATE(),103)



解决方案No.3:格式化(dr [dob],dd-MM-yyyy)。ToString尝试这种格式化函数它会很有用
Solution No.1: If u r using the SQl Server 2008 and above Database U can use the Datatype
as Date.

Solution No.2: U can use the convert(varchar(10),GETDATE(), 105)
convert(varchar(10),GETDATE() , 103)

Solution No.3: Format(dr["dob"], "dd-MM-yyyy").ToString try this format function it will useful


这篇关于如何从数据库中的datetime仅检索日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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