我想要实现的是以下几点: -
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();
}
}