【发布时间】:2017-08-09 11:52:14
【问题描述】:
在我的 win forms 应用程序中,我有 2 个 listView:listView1 和 listView2。
listView1 具有用户选择的数据库表(一个或多个)
当按下按钮时,listView2 会显示与所选表相关的所有列。
当我从 listView1 中选择 2 个表时,嵌套循环超出范围,即使它显示 1 个表的所有列,但由于它超出范围,因此它不会选择第二个表的列。你能告诉我我哪里出错了吗?
这是我的代码:
conn.Open();
SqlCommand sc2 = new SqlCommand("select C_Name, T from (select CONCAT(Table_Schema,'.',Table_Name) T, Concat(Table_Name,'.',Column_Name) C_Name from Information_Schema.columns) as Teo ;", conn);
SqlDataAdapter sda2 = new SqlDataAdapter(sc2);
sda2.Fill(dt);
conn.Close();
DataRow[] foundrows;
string express;
for (int i = 0; i < listView1.CheckedItems.Count; i++)
{
MessageBox.Show(listView1.CheckedItems.Count.ToString());
express="T ='" + listView1.CheckedItems[i].Text+"'";
foundrows = dt.Select(express);
MessageBox.Show(foundrows.Length.ToString());
for (int p = 0; p < foundrows.Length; i++)
{
listView2.Items.Add(foundrows[i][1].ToString());
}
}
【问题讨论】:
-
实际的错误代码是什么,是哪一行抛出的?
-
你的意思是在第二个循环中增加 i 索引吗? for (int p = 0; p
-
@Ehz 明白了。您的第二个循环在应该使用
p时使用i。 -
错误:“索引超出了数组的范围”
标签: c# visual-studio for-loop datatable