现在我想逐行检索一个数据表值。我找到了办法,但我无法得到我想要的结果。它只检索一行记录。这是代码:
MySqlConnection connStr = new MySqlConnection();
connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id";
MySqlCommand cmdSearch = new MySqlCommand(searchPerson, connStr);
connStr.Open();
cmdSearch.Parameters.AddWithValue("@id", "M000001");
MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
dt.Load(dtrRead2);
dtrRead2.Close();
connStr.Close();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
String s = (String)(DataBinder.Eval(e.Item.DataItem, "PersonID"));
String val = "";
for (int i = 0; i < dt.Rows.Count ; i++)
{
//val = dt.Rows[i]["PersonIDB"].ToString();
if (dt.Rows[i]["PersonIDB"].ToString().Equals(s))
e.Item.Visible = false;
else
e.Item.Visible = true;
}
修改
页面加载:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
MySqlConnection connStr = new MySqlConnection();
connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
String searchOverall = "SELECT PersonID, PersonName, Email, Picture FROM Person";
MySqlCommand cmdSearch = new MySqlCommand(searchOverall, connStr);
connStr.Open();
MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
friendRepeater.DataSource = dtrRead2;
friendRepeater.DataBind();
dtrRead2.Close();
dtrRead2 = null;
connStr.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
}
对于ItemDataBound:
protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//String personID = "";
DataTable dt = new DataTable();
try
{
MySqlConnection connStr = new MySqlConnection();
connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id";
MySqlCommand cmdSearch = new MySqlCommand(searchPerson, connStr);
connStr.Open();
cmdSearch.Parameters.AddWithValue("@id", "M000001");
MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
dt.Load(dtrRead2);
dtrRead2.Close();
connStr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
String s = (String)(DataBinder.Eval(e.Item.DataItem, "PersonID"));
String val = "";
for (int i = 0, count = dt.Rows.Count; i < count ; i++)
{
//val = dt.Rows[i]["PersonIDB"].ToString();
//DataRow dr = dt.Rows[i];
if (dt.Rows[i]["PersonIDB"].ToString().Equals(s))
e.Item.Visible = false;
else
e.Item.Visible = true;
}
}
}
答案 0 :(得分:0)
您没有正确填充转发器。它应该是这样的:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>'<%# Eval("PersonIDB")%>'</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
转发器的itemdatabound可能如下所示:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Do anything special you want for the item
}
}
并且不要忘记将数据源绑定到转发器,通常是在页面加载时:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
您的LoadData()方法应如下所示:
private void LoadData()
{
DataTable dt = new DataTable();
MySqlConnection connStr = new MySqlConnection();
connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id";
MySqlCommand cmdSearch = new MySqlCommand(searchPerson, connStr);
connStr.Open();
cmdSearch.Parameters.AddWithValue("@id", "M000001");
MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
dt.Load(dtrRead2);
dtrRead2.Close();
connStr.Close();
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
编辑:
在查看您的编辑后,我建议您更改页面加载中的查询字符串:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
MySqlConnection connStr = new MySqlConnection();
connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;"; PesronIDB
//Change query to include PersonIDB
String searchOverall = "SELECT P.PersonID, P.PersonName, P.Email, P.Picture, CF.PersonIDB FROM Person P LEFT JOIN contactFriend CF ON P.PersonID = CF.PersonID";
MySqlCommand cmdSearch = new MySqlCommand(searchOverall, connStr);
connStr.Open();
MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
friendRepeater.DataSource = dtrRead2;
friendRepeater.DataBind();
dtrRead2.Close();
dtrRead2 = null;
connStr.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
}
在转发器的itemdatabound中,您不需要运行任何查询。