C# 类型化数据集:OleDBDataAdapter 使用类型化数据集中的列名称,而不是 ExcelSheet

发布于 2024-11-19 16:08:30 字数 756 浏览 4 评论 0原文

我正在将 VS 2010 与 C# 一起用于 Windows 窗体应用程序。

我需要将 Excel 工作表中的数据加载到数据集中。我使用数据集设计器创建了数据集,并手动添加了表和列(FirstTable、Column1、Column2)。由于我经常访问这些列,因此使用类型化数据集而不是使用非类型化数据集,代码会干净得多。

当我使用 OleDBDataAdapter 并使用 FirstTable 上的 Fill 填充 DataTable 时,Column1Column2 为空,并且还有两列来自Excel 工作表(ExcelCol1, ExcelCol2)。因此,除非我为 FirstTable 指定了相同的 excel 列名称(如果我将其命名为 ExcelCol1,则不是 Column1Column2 > 和 ExcelCol2),它不会填充我通过设计器创建的 DataColumns。

有什么方法可以告诉 DataSet 忽略来自 Excel 的列并仅填充已定义的 DataColumns 吗?

如果这是不可能的,我可以通过 DataConnection 连接 Excel 工作表来创建数据表的布局吗?我唯一不确定的是 Excel 文件是用户定义的,因此用户浏览到 Excel 文件来填充数据集。但所有这些 Excel 文件的列将始终相同。因此我想使用类型化数据集预设布局。

I'm using VS 2010 with C# for a Windows Form application.

I need to load my data from the Excel sheet onto a DataSet. I created the DataSet using the DataSet designer and I manually added the tables and columns (FirstTable, Column1, Column2). Since I access the columns a lot, the code would be a lot cleaner to have a typed dataset instead of using an untyped one.

When I used the OleDBDataAdapter and populated the DataTable using Fill on FirstTable, Column1 and Column2 were empty and there were two additional columns that were from the Excel sheet (ExcelCol1, ExcelCol2). So unless, I gave the same excel columns names to FirstTable (instead of Column1 and Column2 if I called it ExcelCol1 and ExcelCol2), it would not populate the DataColumns that I created via the designer.

Is there any way to tell the DataSet to ignore the columns coming from Excel and just populate the already defined DataColumns?

IF that is not possible can I somehow connect the Excel sheet via a DataConnection to create the layout of the DataTables? The only thing I'm not sure about is that the excel file is user defined, so the user browses to the excel file to populate the DataSets. BUT the columns for all these excel files will always be the same. Hence I want to preset the layout using a Typed DataSet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-11-26 16:08:30

好吧,我想我知道该怎么做了。我不确定这是否是最干净的方法,因为我仍然需要手动编码来分配列名称,即没有简单的方法来忽略 Excel 列名称,但至少我能够使用类型化数据集。

此链接描述了表和列映射,基本上当您想要使用您自己的列名称,而不是 Excel 中的列名称。

我想指出一些事情,所以我给出了以下代码。

假设我已经通过数据集设计器创建了一个名为 的数据集MyDataSet.xsd。它有一个名为 FirstTable 的表,其中包含列 Column1Column2。然后以下代码可用于表映射

MyDataSet myDS = new MyDataset();
//Some query commands using OleDB to get data from Excel
OleDbDataAdapter myAdapter = new OleDbDataAdapter(<OleDbCommand>);
DataTableMapping tableMap = myAdapter.TableMappings.Add("Table", myDS.FirstTable.TableName);
tableMap.ColumnMappings.Add("ExcelCol1", myDS.FirstTable.Column1.ColumnName);
tableMap.ColumnMappings.Add("ExcelCol2", myDS.FirstTable.Column2.ColumnName);
myAdapter.Fill(myDS);
  1. 我意识到,当您从 Excel 文件读取数据时,在 DataTableMapping 语句中,源名称始终为 "Table" 即使我的工作表名称/表名称Excel 文件上的情况有所不同。
  2. 要利用类型化数据集,您可以使用诸如 myDS.FirstTable.TableName 之类的命令,并对列名称使用相同的命令,而不是

    DataTableMapping tableMap = myAdapter.TableMappings.Add("Table", "FirstTable");

因此,如果您更改 DataSet 中的表或列名称,您可以使用 VS 的重构实用程序。

我希望这有帮助。如果有更干净的解决方案,我将不胜感激任何帮助,但现在这解决了我的问题。

Ok, I think I figured out how to do that. I'm not sure if this is the cleanest method of doing it since I still have to hand-code it to assign the columns names i.e. there is no easy way to ignore the Excel column names but at least I am able to use typed datasets.

This link describes Table and Column mappings which is basically when you would like to use your own column names instead of the ones from Excel.

I would like to point out a few things, so I have given the following code.

Let's assume I have created a DataSet via the DataSet designer called MyDataSet.xsd. It has a table called FirstTable with columns Column1 and Column2. Then the following code can be used for the table mappings

MyDataSet myDS = new MyDataset();
//Some query commands using OleDB to get data from Excel
OleDbDataAdapter myAdapter = new OleDbDataAdapter(<OleDbCommand>);
DataTableMapping tableMap = myAdapter.TableMappings.Add("Table", myDS.FirstTable.TableName);
tableMap.ColumnMappings.Add("ExcelCol1", myDS.FirstTable.Column1.ColumnName);
tableMap.ColumnMappings.Add("ExcelCol2", myDS.FirstTable.Column2.ColumnName);
myAdapter.Fill(myDS);
  1. I realised that when you read data from an Excel file, then in the DataTableMapping statement, the source name is always "Table" even though my sheetname/tablename is different on the Excel file.
  2. To take advantage of the Typed Dataset, you can use commands like myDS.FirstTable.TableName and the same for the columns names instead of,

    DataTableMapping tableMap = myAdapter.TableMappings.Add("Table", "FirstTable");

So if you change the table or column names in the DataSet you can use the refactoring utilities of VS.

I hope this helps. If there is a cleaner solution, I would appreciate any help but for now this fixed my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文