将 CSV 导入数据集 [英] Importing a CSV to Dataset
问题描述
我已经对我的问题进行了一些谷歌搜索,但似乎找不到任何有效的方法.我有一个 .csv(它的格式与 Excel 表格一样,只有字符串),我试图将其导入数据集,但只有其中一列成功导入.其他一切都是 DBNull.
I've done some googling about my issue but I can't seem to find anything that works. I have a .csv (it is formatted just like an excel sheet, with just strings) that I am trying to import into a dataset, but only one of the columns is importing successfully. Everything else is DBNull.
这是我的数据集:
dset.Tables.Add(New DataTable With {.TableName = "ImportedData"})
dset.Tables("ImportDataTable").Columns.Add("ID", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Name", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Type", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc1", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc2", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc3", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc4", GetType(String))
这是我的导入代码:
Dim ImportFileCSV As New OpenFileDialog
With ImportFileCSV
.Title = "Import Overview"
.Filter = "CSV (*.csv)|*.csv"
End With
If ImportFileCSV .ShowDialog = DialogResult.OK Then
Dim ImportPath As String = ImportDetailedOverview.FileName
Dim ImportDirectoryPath As String = Path.GetDirectoryName(ImportPath) & "\"
Dim ImportFileName As String = Path.GetFileName(ImportPath)
Using MyConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & ImportDirectoryPath & ";Extended Properties=""Text;HDR=YES;""")
Using MyCommand As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " & ImportFileName, MyConnection)
MyConnection.Open()
MyCommand.Fill(dset.Tables("ImportedData"))
MyConnection.Close()
End Using
End Using
有没有人有任何见解?
好的,按照艾哈迈德的说法,我正在使用 GenericParser:下面的代码通过 OpenFileDialog 选择文件,然后将返回的表添加到 dset.Tables("ImportDataTable")
Does anyone have any insight?
Okay, so as per Ahmed, I am using the GenericParser:
Below code to select the file via OpenFileDialog and then to add the returned table to dset.Tables("ImportDataTable")
If ImportDetailedOverview.ShowDialog = DialogResult.OK Then
Dim ImportPath As String = ImportDetailedOverview.FileName
dset.Tables("ImportDataTable").Merge(ParseCSV(ImportPath))
我没有收到任何错误.似乎成功完成了,但除 Misc3 之外的所有内容都是空白的.通过绑定来源的力量,我正在显示该信息以进行确认:
I don't get any errors. It seems to finish successfully, but everything except Misc3 is blank. Through the power of binding sources, I am displaying that information to confirm:
CType(fBindingSource.Current, DataRowView)("Name") = drow("Name").ToString
MsgBox(drow("BuildingName").ToString)
CType(fBindingSource.Current, DataRowView)("Type") = drow("Type").ToString
CType(fBindingSource.Current, DataRowView)("Misc1") = drow("Misc1").ToString
CType(fBindingSource.Current, DataRowView)("Misc2") = drow("Misc2").ToString
CType(fBindingSource.Current, DataRowView)("Misc3") = drow("Misc3").ToString
CType(fBindingSource.Current, DataRowView)("Misc4") = drow("Misc4").ToString
CSV 非常简单,它是一个 Excel CSV,例如:col1 有 ID 字段(仅限数字) col2 有名称字段(字符串)等等.
The CSV is very simple, it's an Excel CSV, so for example: col1 has and ID field (numeric only) col2 has a name field (string) so on and so forth.
推荐答案
这里有一个简单的例子说明如何使用 GenericParser 在 VB.NET 中:
Here's a simple example of how to use GenericParser in VB.NET:
首先需要导入命名空间:
First, you need to import the namespace:
Imports GenericParsing
返回数据表:
Private Shared Function ParseCSV(filePath As String) As DataTable
Using parser As New GenericParserAdapter(filePath)
parser.FirstRowHasHeader = True
Return parser.GetDataTable()
End Using
End Function
返回数据集:
Private Shared Function ParseCSV(filePath As String) As DataSet
Using parser As New GenericParserAdapter(filePath)
parser.FirstRowHasHeader = True
Return parser.GetDataSet()
End Using
End Function
参考文献:
这篇关于将 CSV 导入数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!