c#asp.net如何在sql数据库上获取月份和日期

本文关键字:获取 取月份 日期 数据库 sql net c#asp | 更新日期: 2023-09-27 18:25:48

我想做的就是在SQL数据库中只获取月份和日期,并将其绑定到转发器:

正面:

<asp:Repeater ID="rep_content" runat="server">
<ItemTemplate>
<div class="date">
<p><%# Eval("BLG_DATE") %> of <span><%#Eval("BLG_DATE") %></span>
</div>
</ItemTemplate>
</repeater>

背面:

daString = "SELECT * FROM [BLG] INNER JOIN [ACC] ON [BLG].ACC_ID=[ACC].ACC_ID WHERE [BLG].ACC_ID='" + userID + "'";
SqlDataAdapter da = new SqlDataAdapter(daString, conn);
DataTable dt = new DataTable();
da.Fill(dt);    
rep_content.DataSource = dt;
rep_content.DataBind();

BLG表格:

BLG_ID,ACC_ID,BLG_TITLE,BLG_DES,BLG_DATE,BLG_IMG    

输出应为:

23 of JAN

c#asp.net如何在sql数据库上获取月份和日期

使用DatePart获取日期和月份

 daString = "SELECT datepart (dd, BLG_DATE) as DDay,datepart (mm, BLG_DATE) as DMonth
  FROM [BLG] INNER JOIN [ACC] ON [BLG].ACC_ID=[ACC].ACC_ID 
    WHERE [BLG].ACC_ID='" + userID + "'";
 SqlDataAdapter da = new SqlDataAdapter(daString, conn);
 DataTable dt = new DataTable();
 da.Fill(dt);    
 rep_content.DataSource = dt;
 rep_content.DataBind();

 <p><%# Eval("DDay") %> of <span><%#Eval("DMonth") %></span>

这样做吧,

<asp:Repeater ID="rep_content" runat="server">
<ItemTemplate>
<div class="date">
<p><%# Eval("DDay") %> of <span><%#System.Globalization.DateTimeFormatInfo.InvariantInfo.GetAbbreviatedMonthName(Convert.ToInt32(Eval("DMonth")))%></span>
</div>
</ItemTemplate>
</repeater>