看看这段源码吧
-----------------------------------------------------------------------
rivate void tVBook_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
string sqlSel="";
lVBookInfo.Clear();
string NodeText=tVBook.SelectedNode.Text;
if(NodeText=="图书信息")
sqlSel="SELECT * FROM Book_Info";
else
sqlSel="SELECT * FROM Book_Info WHERE 类别='"+NodeText+"'";
OleDbConnection Conn=new OleDbConnection(DBConn.ConnStr);
OleDbCommand ODBCmd=new OleDbCommand(sqlSel,Conn);
try
{
Conn.Open();
OleDbDataReader ODBdr=ODBCmd.ExecuteReader();
int cols=ODBdr.FieldCount;
lVBookInfo.BeginUpdate();
for(int i=0;i<cols;i++)
{
lVBookInfo.Columns.Add(ODBdr.GetName(i),100,HorizontalAlignment.Left);
}
string[] lvitem=new string[cols];
object[] values=new object[cols];
while(ODBdr.Read())
{
ODBdr.GetValues(values);
for(int j=0;j<values.Length;j++)
lvitem[j]=values[j].ToString();
ListViewItem lvi=new ListViewItem(lvitem);
lVBookInfo.Items.Add(lvi);
}
lVBookInfo.EndUpdate();
ODBdr.Close();
}
catch(Exception E)
{
MessageBox.Show("异常出现在:"+E.Message,"异常");
}
finally
{
Conn.Close();
}
}