我有一个DataGridView
,它从我分离的网络的每个磁盘中填充数据。我想将它插入到我的数据库中,但我不知道如何验证我在DataGridView
中有多少行。
这是我已有的代码:
try
{
con = new OleDbConnection(cs.DBConn);
con.Open();
string queryInsert = @"INSERT INTO tblComputers
(Partition1Disk, Type1Disk, TotalSpace1Disk,
UseSpace1Disk, FreeSpace1Disk, PercentageUse1Disk, Partition2Disk, Type2Disk, TotalSpace2Disk,
UseSpace2Disk, FreeSpace2Disk, PercentageUse2Disk, Partition3Disk, Type3Disk, TotalSpace3Disk,
UseSpace3Disk, FreeSpace3Disk, PercentageUse3Disk,Partition4Disk, Type4Disk, TotalSpace4Disk,
UseSpace4Disk, FreeSpace4Disk, PercentageUse4Disk)
VALUES
(@Partition1Disk, @Type1Disk, @TotalSpace1Disk, @UseSpace1Disk, @FreeSpace1Disk, @PercentageUse1Disk,
@Partition2Disk, @Type2Disk, @TotalSpace2Disk, @UseSpace2Disk, @FreeSpace2Disk, @PercentageUse2Disk,
@Partition3Disk, @Type3Disk, @TotalSpace3Disk, @UseSpace3Disk, @FreeSpace3Disk, @PercentageUse3Disk,
@Partition4Disk, @Type4Disk, @TotalSpace4Disk, @UseSpace4Disk, @FreeSpace4Disk, @PercentageUse4Disk)";
cmd = new OleDbCommand(queryInsert);
cmd.Connection = con;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
cmd.Parameters["@Partition1Disk"].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
cmd.Parameters["@Type1Disk"].Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
cmd.Parameters["@TotalSpace1Disk"].Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
cmd.Parameters["@UseSpace1Disk"].Value = dataGridView1.Rows[i].Cells[3].Value.ToString();
cmd.Parameters["@FreeSpace1Disk"].Value = dataGridView1.Rows[i].Cells[4].Value.ToString();
cmd.Parameters["@PercentageUse1Disk"].Value = dataGridView1.Rows[i].Cells[5].Value.ToString();
cmd.Parameters["@Partition2Disk"].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
cmd.Parameters["@Type2Disk"].Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
cmd.Parameters["@TotalSpace2Disk"].Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
cmd.Parameters["@UseSpace2Disk"].Value = dataGridView1.Rows[i].Cells[3].Value.ToString();
cmd.Parameters["@FreeSpace2Disk"].Value = dataGridView1.Rows[i].Cells[4].Value.ToString();
cmd.Parameters["@PercentageUse2Disk"].Value = dataGridView1.Rows[i].Cells[5].Value.ToString();
cmd.Parameters["@Partition3Disk"].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
cmd.Parameters["@Type3Disk"].Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
cmd.Parameters["@TotalSpace3Disk"].Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
cmd.Parameters["@UseSpace3Disk"].Value = dataGridView1.Rows[i].Cells[3].Value.ToString();
cmd.Parameters["@FreeSpace3Disk"].Value = dataGridView1.Rows[i].Cells[4].Value.ToString();
cmd.Parameters["@PercentageUse3Disk"].Value = dataGridView1.Rows[i].Cells[5].Value.ToString();
cmd.Parameters["@Partition4Disk"].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
cmd.Parameters["@Type4Disk"].Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
cmd.Parameters["@TotalSpace4Disk"].Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
cmd.Parameters["@UseSpace4Disk"].Value = dataGridView1.Rows[i].Cells[3].Value.ToString();
cmd.Parameters["@FreeSpace4Disk"].Value = dataGridView1.Rows[i].Cells[4].Value.ToString();
cmd.Parameters["@PercentageUse4Disk"].Value = dataGridView1.Rows[i].Cells[5].Value.ToString();
}
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
throw;
}
问题是我的网络中的某些计算机只有一个分区,当它发生时,应用程序将显示一个错误,指出索引超出范围。我可以做任何验证,以确定DataGridView
中只有一行或最多4行,并且根据该行在该列中插入null?