mysqldatareader获取整行数据给datarow_在C#中的oledbdatareader中读取第n行

I have a excel sheet showing employees salary details in department wise.

Just I need to save these details in to MySql .

When I use oledbdatareader,it reads from the first row. But I have to choose from different rows. Below im showing sample excel sheet

Dept Software

Name Gross Deductions NetPay

AAA 10000 2000 8000

BBB 10000 1000 9000

Dept HR

Name Gross Deductions NetPay

CCC 20000 1000 19000

Here each line is a row. I have to take third row ,fourth row and then last row (in this example).

AAA 10000 2000 8000

BBB 10000 1000 9000

CCC 20000 1000 19000

How can i achieve this?

I tried like this.

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;

}

}

Talk1:

No built in support from DataReader to read a specific row, you have to do modular arithmetic yourself to achieve this..

Solutions1

You can use OleDbDataAdapter to read all rows to fill a data set. And then select a row by index in the datatable of this dataset. An example to use OleDbDataAdapter is,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值