gpt4 book ai didi

c# - 在 C# 中读取 oledbdatareader 中的第 n 行

转载 作者:行者123 更新时间:2023-11-29 02:55:53 24 4
gpt4 key购买 nike

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

最佳答案

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

DataAdapter.Fill(Dataset)

关于c# - 在 C# 中读取 oledbdatareader 中的第 n 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30662954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com