使用其他表的值在c#中访问表的值

本文关键字:访问表 其他 | 更新日期: 2023-09-27 18:02:45

我有两个名为myfriends和imageslikes的表。在我的代码中,我试图访问表myfriends的列id2,并基于中的每个值,我想访问imageslikes表的记录。但与我的代码,我只得到值作为输出中继器可能是因为它被覆盖。

访问我想要的所有记录的可能解决方案是什么?有人能帮忙吗?

        Query = "select id2 from myfriends where id1 = '" + Session["id"] + "'";
        adap = new SqlDataAdapter(Query, con);
        ds = new DataSet();
        adap.Fill(ds);
        dt = ds.Tables[0];
        dr = dt.Rows[0];
        foreach (DataRow row in dt.Rows)
        {
            string id2 = row["id2"].ToString();
            Query = "select * from imageslikes where likedby = '" + id2 + "'";
            adap = new SqlDataAdapter(Query, con);
            ds = new DataSet();
            adap.Fill(ds);
            DataList4.DataSource = ds;
            DataList4.DataBind();

        }

使用其他表的值在c#中访问表的值

        Query = "select * from imageslikes where likedby=(select id2 from myfriends where id1 = '" + Session["id"] + "') ";
    Query = "select id2 from myfriends where id1 = '" + Session["id"] + "'";
adap = new SqlDataAdapter(Query, con);
ds = new DataSet();
adap.Fill(ds);
dt = ds.Tables[0];
dr = dt.Rows[0];
foreach (DataRow row in dt.Rows)
{
    string id2 = row["id2"].ToString();
    Query = "select * from imageslikes where likedby = '" + id2 + "'";
    adap = new SqlDataAdapter(Query, con);
    ds = new DataSet();
    adap.Fill(ds);
    DataList4.DataSource = ds;
    DataList4.DataBind();
    //Added this line
    ds = new DataSet();
    //if you need both records related to id1 and id2 then use
    Query = "select * from imageslikes where likedby in ('" + id2 + "', '" + id1 + "')";
    //Query = "select * from imageslikes where likedby = '" + id2 + "'";
    adap = new SqlDataAdapter(Query, con);
    ds = new DataSet();
    adap.Fill(ds);
    Repeater3.DataSource = ds;
    Repeater3.DataBind(); 

}