我要从查询的两个日期之间获取数据,但问题是文本框以YYYY / MM / dd格式获取数据,我需要将其转换为dd / MM / yyyy。
Start Date: <asp:TextBox ID="txtstart" runat="server" TextMode="Date" ></asp:TextBox>
End Date: <asp:TextBox ID="txtend" runat="server" TextMode="Date"></asp:TextBox>
<p>
<asp:Button ID="btnfetch" runat="server" Text="Button" OnClick="btnfetch_Click1" />
</p>
<div>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
</div>
using (SqlCommand cmd = new SqlCommand(
"SELECT Name,Present from AttendanceRecords " +
"where Date between '" + txtstart.Text + "' and '" + txtend.Text + "' ", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
GridView2.DataSource = dt;
GridView2.DataBind();
}
else
{
lbldatercrd.Text = "NOT FOUND";
}
答案 0 :(得分:1)
阿迪尔(Adil)
DataAdapter将根据需要打开和关闭连接。
DateTime start, end;
if (DateTime.TryParse(txtStart.Text, out start) && DateTime.TryParse(txtEnd.Text, out end))
{
string cmd = @"SELECT Name,Present
from AttendanceRecords
where [Date] between @start and @end";
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
da.SelectCommand.Parameters.Add("@start", SqlDbType.DateTime).Value = start;
da.SelectCommand.Parameters.Add("@end", SqlDbType.DateTime).Value = end;
DataTable dt = new DataTable();
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
else
{
lbldatercrd.Text = "NOT FOUND";
}