我正在处理需要从列表视图中进行选择的事情,然后根据第 1 列(或 ii)从 SQL Server 数据库中选择项目,然后将其重写到本地数据表以处理成文本文件。这是我到目前为止所拥有的:
var con = new SqlCeConnection(ConfigurationManager.ConnectionStrings[Connection.ConnectionStrings.CurrentConnection].ConnectionString);
foreach (ListViewItem item in LSTAsset.SelectedItems)
{
for (int ii = 0; ii < LSTAsset.SelectedItems.Count; ii++)
{
string select = LSTAsset.SelectedItems[ii].Text;
if (con != null && con.State == ConnectionState.Closed)
{
con.Open();
}
var cmd = new SqlCeCommand("Select assetname, serial from asset where assetname like '%" + select + "%'", con);
SqlCeDataAdapter dataadapt = new SqlCeDataAdapter(cmd);
//DataTable datatable = new DataTable();
if (ii == 0)
{
dataadapt.Fill(steve.iTable);
}
if (ii < 0)
{
dataadapt.Update(steve.iTable);
}
}
}
Now my issue being, it seems to double the selected items when more than one item is selected. 假设您选择了 2 个项目,它现在运行代码 4 次,并在数据表中为您提供 4 行。为什么代码加倍?它稍后会破坏输出文件,因为它会在表中写入 2 倍所需的行。