// Execute and return the rows in the data reader object OleDbDataReader dataReader; dataReader = command.ExecuteReader(CommandBehavior.CloseConnection); int nFields = dataReader.FieldCount; // Setup the columns in the listview using the fields in the table lstDatas.Clear(); for (int i = 0; i<nFields; i++) { lstDatas.Columns.Add(dataReader.GetName(i), 100, HorizontalAlignment.Left); } // Fill the rows in the listview using the data in the rows int nRow = 0; while (dataReader.Read()) { // Create an array of subitems for quick insertion // The subitems will be all fields in the row except for // the first field String [] subitems = new String[nFields]; for (int i = 0; i < nFields; i++) { subitems[i] = dataReader[i].ToString(); } // Insert a new item into the listview, and add the subitems at // the same time. The item will be the first field in the row ListViewItem item = new ListViewItem(subitems, -1); lstDatas.Items.Add(item); ++nRow; } dataReader.Close(); } finally { // Close the connection if necessary if (Connection.State == System.Data.ConnectionState.Open) Connection.Close(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)