获取基于日期时间的值
本文关键字:时间 日期 于日期 获取 | 更新日期: 2023-09-27 18:15:37
我想实现的是:-
1-从数据库中获取具有当前日期(今天)的记录。2-如果数据库没有今天的记录,我希望它从前一天得到它。3-如果前一天没有任何记录,我希望它从前一天开始获取它们,以此类推。
try
{
string date = DateTime.Now.ToShortDateString();
conn.Open();
//string str = "insert into Table1 (title , date_ ,www, cat) values (' " + TextBox1.Text + "','" + DateTime.Now.ToShortDateString() + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Text + "')";
////string str = "INSERT INTO Table1 (title,date_,www ) values ('ddddddd','aaaaaaa','qqqqqq')";
string str =
//"SELECT from table1 WHERE cat = 1 and datee='" + date + "'ORDER BY datee";
"SELECT * FROM table1 WHERE cat = 1 and datee = @dt ORDER BY datee DESC ";
SqlCommand objcmd = new SqlCommand(str, conn);
objcmd.Parameters.AddWithValue("@dt", date);
SqlDataAdapter da1 = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
da1.Fill(dt);
//DataRow dr = new DataRow();
//DataRow dr = ds.Tables[0].Rows[0];
int id = 1;
foreach (DataRow dr in dt.Rows)
{
if (id == 1)
{
ll1.Text = dr[1].ToString();
l1.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 2)
{
ll2.Text = dr[1].ToString();
l2.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 3)
{
ll3.Text = dr[1].ToString();
l3.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 4)
{
ll4.Text = dr[1].ToString();
l4.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 5)
{
ll5.Text = dr[1].ToString();
l5.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 6)
{
ll6.Text = dr[1].ToString();
l6.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 7)
{
ll7.Text = dr[1].ToString();
l7.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 8)
{
ll8.Text = dr[1].ToString();
l8.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 9)
{
ll9.Text = dr[1].ToString();
l9.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 10)
{
ll10.Text = dr[1].ToString();
l10.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 11)
{
ll11.Text = dr[1].ToString();
l11.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 12)
{
ll12.Text = dr[1].ToString();
l12.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 13)
{
ll13.Text = dr[1].ToString();
l13.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 14)
{
ll14.Text = dr[1].ToString();
l14.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 15)
{
ll15.Text = dr[1].ToString();
l16.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 16)
{
ll16.Text = dr[1].ToString();
l16.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 17)
{
ll17.Text = dr[1].ToString();
l17.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 18)
{
ll18.Text = dr[1].ToString();
l18.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 19)
{
ll19.Text = dr[1].ToString();
l19.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 20)
{
ll20.Text = dr[1].ToString();
l20.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 21)
{
ll21.Text = dr[1].ToString();
l21.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 22)
{
ll22.Text = dr[1].ToString();
l22.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 23)
{
ll23.Text = dr[1].ToString();
l23.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
if (id == 24)
{
ll24.Text = dr[1].ToString();
l24.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
id = id + 1;
}
}
catch (Exception ex)
{
lblError.Text = "فشل في إحظار الأخبار";
}
finally
{
conn.Close();
}
}
int id = 1;//???????
foreach (DataRow dr in dt.Rows)
{
if (id == 1)//???????
{
ll1.Text = dr[1].ToString();
l1.ImageUrl = @"photo'" + dr[1].ToString() + @".jpg";
}
您总是检查ID是否为ID=1而不是使用这个:
if ( dr[1] == id)
{
...
}
id++
.
.
.
我建议你更新你的SQL查询做你的工作,避免2数据库命中,你可以这样做:
string str =
"SELECT * FROM table1 WHERE cat = 1 and datee = @dt ORDER BY datee DESC " +
"UNION SELECT * FROM table1 WHERE cat = 1 and datee < @dt AND (SELECT COUNT(*) FROM table1 WHERE cat = 1 and datee = @dt) = 0 ORDER BY datee DESC";
您可以看到,如果这些结果的计数为0,则将日期之前的所有结果连接到结果中(这意味着如果没有选择,则选择该日期之前的结果)
下面是一些代码,用于获取整个第一天中有任何行(按时间向后)的所有项目。这假设SQL Server 2005或更高版本(你没有说什么DBMS & &;你正在使用的版本,请这样做!):
WITH Data AS (
SELECT TOP 1 WITH TIES
*
FROM dbo.table1
WHERE
cat = 1
AND datee >= DateAdd(day, -5, @dt)
ORDER BY
Convert(date, datee) DESC
)
SELECT *
FROM Data
ORDER BY datee DESC
;
如果你可以在你的前端排序行(你可能应该),那么你不需要额外的,外部查询。使用TOP 1 WITH TIES
语法,您不能向内部ORDER BY
子句添加更多表达式,因为这会改变所选择的内容。
您可能还应该只选择所需的列,而不是表中的所有列。