我有一张桌子,上面有儿童的照片。现在我想用他/她的Child_ID读取特定孩子的所有图像URL。问题是我不知道如何从读者那里填写收藏品。您可以在下面找到我正在使用的代码;
private void Bindlist()
{
string id = Request.QueryString["ID"];
using (SqlConnection connection = new SqlConnection(conString))
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand("GetChildImages", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@ChildID", id));
using (SqlDataReader reader = command.ExecuteReader())
{
var list = new Collection<MyImage>
{
};
}
}
} catch (Exception)
{
Response.Redirect("Error.htm", false);
}
}
Repeater1.DataSource = array;
Repeater1.DataBind();
}
public class MyImage
{
public string ImageSRC { get; set; }
public string ThumbImageSRC { get; set; }
}
答案 0 :(得分:1)
使用阅读器的Read
方法:
using (SqlDataReader reader = command.ExecuteReader())
{
var list = new Collection<MyImage>();
while(reader.Read())
{
// create new `MyImage` from reader - you now have access to the columns
// guessing at names of the columns:
list.Add(new MyImage { ImageSRC = (string)reader["URL"],
ThumbImageSRC = (string)reader["URL_Small"] });
}
}
答案 1 :(得分:1)
using (SqlDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
MyImage i = new MyImage();
i.YourPropery1 = (string) reader["Column1"];
i.YourPropery2 = (int) reader["Column2"];
...
list.add(i);
}
}
答案 2 :(得分:1)
尝试使用以下代码:
private void Bindlist()
{
string id = Request.QueryString["ID"];
List<MyImage> listImage=new List<MyImage>();
using (SqlConnection connection = new SqlConnection(conString))
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand("GetChildImages", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@ChildID", id));
using (SqlDataReader reader = command.ExecuteReader())
{
while(reader.Read())
{
MyImage image=new MyImage();
image.ImageSRC=reader.GetString(0);
image.ThumbImageSRC=reader.GetString(1);
listImage.Add(image);
}
}
}
}
catch (Exception)
{
Response.Redirect("Error.htm", false);
}
}
Repeater1.DataSource = listImage;
Repeater1.DataBind();
}
public class MyImage
{
public string ImageSRC { get; set; }
public string ThumbImageSRC { get; set; }
}
答案 3 :(得分:0)
你可以尝试
while(reader.read())
{
var list = new Collection<MyImage>
{
ImageSRC =reader.GetString(columnIndex),
ThumbImageSRC=reader.GetString(columnIndex)
};
}