折腾了一天都没有搞定的C# ListViewItem控件查询问题
每次走到 循环读出数据哪里就自动跳出,下面有标识,从中午开始一直到现在2010年12月29日 16:57:44为止,都没有搞定,--!
//查询方法
private void FillListView()
{
int id;
string tiem;//录入时间
int blandID =0;//电脑品牌
string bland = "";
string CPU = "";//CPU型号
int CPUID =0;
string Type;//电脑型号
string memory;//内存容量
string hardDisk;//硬盘容量
string price;//电脑价格
try
{
//得到电脑品牌 ID
string sqlD = string.Format("SELECT ID, Bland FROM BlandType WHERE Bland = '{0}'", textBox1.Text);
SqlCommand commandD = new SqlCommand(sqlD, dbhelpClass1.conn);
dbhelpClass1.conn.Open();
SqlDataReader dataReaderD = commandD.ExecuteReader();
if (dataReaderD.Read())
{
blandID =Convert.ToInt32(dataReaderD[0]);
bland =Convert.ToString(dataReaderD[1]);
}
dataReaderD.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
finally
{
dbhelpClass1.conn.Close();
}
try
{
//得到电脑型号
string sqlB = string.Format("SELECT Bland,CPU FROM ComputerInfo WHERE Bland ={0}", blandID);
SqlCommand commandB = new SqlCommand(sqlB, dbhelpClass1.conn);
dbhelpClass1.conn.Open();
SqlDataReader dataReaderB = commandB.ExecuteReader();
if (dataReaderB.Read())
{
CPUID =Convert.ToInt32(dataReaderB[1]);
}
dataReaderB.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
finally
{
dbhelpClass1.conn.Close();
}
try
{
//得到"电脑品牌" "CPU型号" ID
string sqlA = string.Format("SELECT ID,CPUtype FROM CPUtype WHERE ID ={0}",CPUID);
SqlCommand commandA = new SqlCommand(sqlA, dbhelpClass1.conn);
dbhelpClass1.conn.Open();
SqlDataReader dataReaderA = commandA.ExecuteReader();
if (dataReaderA.Read())
{
CPU = Convert.ToString(dataReaderA[1]);//CPU型号ID
}
dataReaderA.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
finally
{
dbhelpClass1.conn.Close();
}
try
{
string sql = string.Format("SELECT ID,Tiem,Bland,Type,Memory,HardDisk,Price FROM ComputerInfo WHERE Bland={0}",blandID);
SqlCommand command = new SqlCommand(sql, dbhelpClass1.conn);
dbhelpClass1.conn.Open();
SqlDataReader dataReader = command.ExecuteReader();
//清空表中内容
listViewA.Items.Clear();
if (!dataReader.Read())
{
MessageBox.Show("没有你要找的!!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
主要问题在这里,[color=#0000FF]在这里,在这里
while(dataReader.Read())
{
id = (int)dataReader["ID"];
tiem = (string)dataReader["Tiem"];
Type = (string)dataReader["Type"];
memory = (string)dataReader["Memory"];
hardDisk = (string)dataReader["HardDisk"];
price = (string)dataReader["Price"];
ListViewItem lviBland = new ListViewItem(Convert.ToString(id));
lviBland.Tag = (int)dataReader["ID"];
listViewA.Items.Add(lviBland);
lviBland.SubItems.AddRange(new string [] {bland,Type,CPU,memory,hardDisk,price,tiem});
}
}
dataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
finally
{
dbhelpClass1.conn.Close();
}
}