开发者

How to get column names with ODP.NET DataAdapter filling a DataSet in C#?

开发者 https://www.devze.com 2023-04-02 08:40 出处:网络
I\'m filling a Data Table use ODP.NET\'s OracleDataAdapter. I\'d like to pass along the column headers too. Any idea how to go about this? Thanks!

I'm filling a Data Table use ODP.NET's OracleDataAdapter. I'd like to pass along the column headers too. Any idea how to go about this? Thanks!

OracleDataAdapter oda = new OracleDataAdapter(oc);

            oda.Fill(dt);

            FileInfo 开发者_如何转开发outFile = new FileInfo(outputPath + "out.xlsx");
            using (ExcelPackage pck = new ExcelPackage(outFile))
            {


                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Netstat");
                for (int row = 1; row <= dt.Rows.Count; row++)
                {
                    for (int column = 1; column <= dt.Columns.Count; column++)
                    {
                        ws.Cells[row, column].Value = dt.Rows[row - 1][column - 1];
                    }
                }

                pck.Save();


Not sure how you are filling the DataSet but if you use the "standard way" i.e.

string MyTableName = "customers";
MyAdapter.Fill(MyDataSet, MyTableName);

THEN the column names are already in the DataSet i.e. MyDataSet.Tables [MyTableName].Columns[0].ColumnName .

This is a standard ADO.NET feature, nothing special about ODP.NET - see the MSDN reference at http://msdn.microsoft.com/en-us/library/bh8kx08z%28v=VS.100%29.aspx and http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx

EDIT - AFTER the OP added source code:

In your code you aren't using DataSet but DataTable so you can access the column names by dt.Columns[column].ColumnName .

0

精彩评论

暂无评论...
验证码 换一张
取 消