比较dataSet值

时间:2014-02-18 04:24:43

标签: c# dataset

我使用此代码来显示要编辑的图像:

protected void Repeater_Outer_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;

    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater Inner = (Repeater)item.FindControl("image_Repeater");
        HiddenField Inner_Id = (HiddenField)item.FindControl("HiddenField_Id");

        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbcnx"].ToString());
        MySqlCommand cmdNew = new MySqlCommand();

        cmdNew.Connection = conn;
        cmdNew.Parameters.AddWithValue("@id", Inner_Id.Value);
        cmdNew.CommandText = "SELECT * FROM images WHERE FK_album = @id";

        conn.Open();
        Inner.DataSource = cmdNew.ExecuteReader();
        Inner.DataBind();

        Label Label_Amount = (Label)item.FindControl("Label_Amount");
        Label_Amount.Text = Convert.ToString(Inner.Items.Count);
        conn.Close();
    }
}

我的问题是,每次转发器运行时,它都连接到数据库,这使得页面加载时间很长(例如10秒) 所以我想将数据集或通用List填充到数据绑定中。我试过这个:

protected static List<string> dataSetImages(){
        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbcnx"].ToString());
        string sql = "select * from images";
        MySqlCommand cmd = new MySqlCommand( sql, conn);
        conn.Open();
        MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(cmd); 
        DataSet dataSet = new DataSet();
        sqlDataAdapter.Fill(dataSet);
        conn.Close();
        List<string> imageList = new List<string>();
        for (int i = 0; i <= dataSet.Tables[0].Rows.Count - 1; i++)
        {
                string id = dataSet.Tables[0].Rows[i].ItemArray[0].ToString();
                string img_name = dataSet.Tables[0].Rows[i].ItemArray[1].ToString();
                string img_alt = dataSet.Tables[0].Rows[i].ItemArray[2].ToString();
                string FK_album = dataSet.Tables[0].Rows[i].ItemArray[3].ToString();
            imageList.Add(id);
            imageList.Add(img_name);
            imageList.Add(img_alt);
            imageList.Add(FK_album);
        }
        return imageList;
}

我如何使用它并将FK_album与Inner_Id.Value进行比较?

2 个答案:

答案 0 :(得分:0)

如果你可以上课,那就更好了

public class ImageDto
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Alt { get; set; }
    public string FK_album { get; set; }

}

然后

protected static List<ImageDto> dataSetImages(){
   List<ImageDto> imageList = new List<ImageDto>();
   // add items 
   return imageList;

}

然后您可以调用上面的方法并获取ImageDto

的列表
List<ImageDto> images  = dataSetImages();

您可以使用Repeater_Outer_ItemDataBound方法执行以下操作

Inner.DataSource = images.Where(i=>i.FK_album == Inner_Id.Value).ToList();

答案 1 :(得分:0)

如果您坚持使用RowFilter approach,那么这就是您要找的。

string expression = String.Format("FK_album = {0}", Inner_Id.Value);
DataRow[] filteredRows = imageList.Tables[0].Select(expression);

以下是关于DataView RowFilter Syntax的文章。