現在我要逐行檢索一個數據表值。我找到了一種方法來完成它,但我無法得到我想要的結果。它只檢索一條記錄。下面是代碼:按行檢索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();
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;
}
編輯
頁面加載:
對於的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;
}
}
}
您可能想從連接字符串中刪除用戶名/密碼(並更改服務器上的密碼...) – dav1dsm1th