C# 在c中读取OLEDB数据读取器中的第n行#

C# 在c中读取OLEDB数据读取器中的第n行#,c#,mysql,oledbdatareader,C#,Mysql,Oledbdatareader,我有一张excel表格,以部门为单位显示员工的工资明细。 只是我需要将这些细节保存到MySql中 当我使用oledbdatareader时,它从第一行开始读取。但我必须从不同的行中选择。下面我展示了示例excel表 Dept Software Name Gross Deductions NetPay AAA 10000 2000 8000 BBB 10000 1000 9000 Dept HR

我有一张excel表格,以部门为单位显示员工的工资明细。 只是我需要将这些细节保存到MySql中

当我使用oledbdatareader时,它从第一行开始读取。但我必须从不同的行中选择。下面我展示了示例excel表

Dept    Software         
Name    Gross   Deductions  NetPay
AAA    10000    2000        8000
BBB    10000    1000        9000
Dept    HR       
Name    Gross   Deductions  NetPay
CCC    20000        1000     19000
这里每行是一行。我必须取第三行、第四行,然后是最后一行(在本例中)

我怎样才能做到这一点? 我试过了

protected void Button2_Click(object sender, EventArgs e)
  {           

   string path = "C:\\Payslip.xls";
   string query = "SELECT * FROM [Sheet3$]";
   OleDbConnection conn = new OleDbConnection();
   conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
   conn.Open();              

try
{
        OleDbCommand ocmd = new OleDbCommand(query, conn);
        OleDbDataReader odr = ocmd.ExecuteReader();             

        while (odr.Read())
        {                               
                name = odr[0].ToString();            
                gross = odr[1].ToString();     
                ded = odr[2].ToString();                
                net = odr[3].ToString();        

                connection = new MySqlConnection(connectionString);
                connection.Open();

                String sQuery = "insert into salary (EmployeeName, Gross) values(@a, @b)";

                MySqlCommand cmd = new MySqlCommand(sQuery, connection);
                cmd.Parameters.AddWithValue("a", name);
                cmd.Parameters.AddWithValue("b", gross); 

                cmd.ExecuteNonQuery();  
                connection.Close();
            }    
      }    
      catch (Exception ex)    
      {
          Label1.Text = ex.Message;                        
      }                 
}

可以使用OleDbDataAdapter读取所有行以填充数据集。然后在此数据集的datatable中按索引选择一行。使用OleDbDataAdapter的一个示例是


没有来自
DataReader的内置支持要读取特定行,您必须自己进行模块化运算才能实现这一点。。
protected void Button2_Click(object sender, EventArgs e)
  {           

   string path = "C:\\Payslip.xls";
   string query = "SELECT * FROM [Sheet3$]";
   OleDbConnection conn = new OleDbConnection();
   conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
   conn.Open();              

try
{
        OleDbCommand ocmd = new OleDbCommand(query, conn);
        OleDbDataReader odr = ocmd.ExecuteReader();             

        while (odr.Read())
        {                               
                name = odr[0].ToString();            
                gross = odr[1].ToString();     
                ded = odr[2].ToString();                
                net = odr[3].ToString();        

                connection = new MySqlConnection(connectionString);
                connection.Open();

                String sQuery = "insert into salary (EmployeeName, Gross) values(@a, @b)";

                MySqlCommand cmd = new MySqlCommand(sQuery, connection);
                cmd.Parameters.AddWithValue("a", name);
                cmd.Parameters.AddWithValue("b", gross); 

                cmd.ExecuteNonQuery();  
                connection.Close();
            }    
      }    
      catch (Exception ex)    
      {
          Label1.Text = ex.Message;                        
      }                 
}