【问题标题】:reading n-th row in oledbdatareader in c#在 c# 中读取 oledbdatareader 中的第 n 行
【发布时间】:2015-08-20 04:37:53
【问题描述】:

我有一个 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

这里每一行都是一行。我必须取第三行、第四行和最后一行(在本例中)。

AAA    10000    2000        8000
BBB    10000    1000        9000
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;                        
      }                 
}

【问题讨论】:

  • 没有来自DataReader 的内置支持来读取特定行,您必须自己进行模运算才能实现这一点..

标签: c# mysql oledbdatareader


【解决方案1】:

您可以使用 OleDbDataAdapter 读取所有行以填充数据集。然后在这个数据集的数据表中按索引选择一行。使用 OleDbDataAdapter 的一个例子是,

DataAdapter.Fill(Dataset)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    相关资源
    最近更新 更多