这最终将在PLC的原始Windows环境中运行,因此我在Studio 2013中使用Visual Basic .NET 4.0。
无论如何,这里的挑战我希望一旦开始就相当简单:我有3个MDB,每个包含1个表和1个CSV文件,我需要从中导入数据并提供各种查看/编辑功能。
这些MDB本质上是各种"程序设置的参数列表。由PLC运行,称为"食谱"。有超过200个参数,但由于某种原因,它们被限制为每个文件100个,因此他们必须创建3个MDB。但它们基本上代表了一长串参数。因此,例如,PLC可以执行4种不同的格式或配方,每种格式或配方都具有唯一的" IndexName"键。所以在第一个MDB中,表有4行106列,第二行有4行和110列。
Here's a screenshot of the table in Access
"导出Demo.zip"在这篇文章上 http://www.vbforums.com/showthread.php?719287-RESOLVED-mdb-database-to-csv-file 看起来很有希望,但它在VB6中有点超越我适应我的环境。
我可以为每个源MDB / CSV创建一个DataSource / DataSet,但我的目标是使用来自所有这些源的Data填充单个DataGrid对象。我目前正在研究如何将数据整合到一个DataTable中。
答案 0 :(得分:0)
从各种来源中抽出一些零碎的东西,我设法让这个工作。特别有用的是DataTable.Merge函数,花了一些时间来找到它。
不确定此代码对其他人有多么有用,或者它是否符合stackoverflow礼仪。我很乐意相应地修复,但我确实希望分享我管理的内容。
显然这不是完整的代码,但希望是最相关的代码。如果需要,我很乐意提供更多的作品。代码评论/改进欢迎,我只是一个初学者!
Sub LoadDataIntoDataGrid()
'Loads CSV Table, Transpose & Load 3 MDB tables, Capture each Table Params, & Merge all 3 into 1
'Adjusts the merged table according to the csv file
Dim CSVTable As DataTable, MDB0Table As DataTable, MDB1Table As DataTable, MDB2Table As DataTable, _
CRLTable As DataTable
Dim CSVRow As DataRow
Dim removeRows As New List(Of String)
'Populate CSV Table
CSVTable = ReadFromCSV("CSVFile.csv")
'Populate 3 MDB tables with transposed Data: Rows are parameters
MDB0Table = ReadFromMDB("First.mdb", "SELECT * from Table1")
MDB1Table = ReadFromMDB("Second.mdb", "SELECT * from Table2")
MDB2Table = ReadFromMDB("Third.mdb", "SELECT * from Table3")
'Put each Parameter name into MDBTableParams
For Each ParamName In MDB0Table.Rows
CRLForm.MDBTableParams(0).Add(ParamName(0))
Next
For Each ParamName In MDB1Table.Rows
CRLForm.MDBTableParams(1).Add(ParamName(0))
Next
For Each ParamName In MDB2Table.Rows
CRLForm.MDBTableParams(2).Add(ParamName(0))
Next
'Set Primary Keys
MDB0Table.PrimaryKey = New DataColumn() {MDB0Table.Columns("NamePar")}
MDB1Table.PrimaryKey = New DataColumn() {MDB1Table.Columns("NamePar")}
MDB2Table.PrimaryKey = New DataColumn() {MDB2Table.Columns("NamePar")}
CSVTable.PrimaryKey = New DataColumn() {CSVTable.Columns("NamePar")}
'Merge all 3 MDB Tables into 1
CRLTable = MDB0Table
CRLTable.Merge(MDB1Table)
CRLTable.Merge(MDB2Table)
'For each row in the merged table, either mark for removal or add the description from the CSV file
CRLTable.Columns.Add("Desc", GetType(String))
CRLTable.Columns("Desc").SetOrdinal(1) 'Make Description second column (after "NamePar")
For Each CRLRow In CRLTable.Rows
CSVRow = CSVTable.Rows.Find(CRLRow("NamePar"))
If CSVRow Is Nothing Then
removeRows.Add(CRLRow("NamePar")) 'tag rows to be removed due to missing in CSV
Else
CRLRow("Desc") = CSVRow("DESC_" & CRLForm.ComboBoxLanguage.Text)
End If
Next
'Remove Rows tagged for removal due to absence from CSV file
For Each row In removeRows
CRLTable.Rows.Remove(CRLTable.Rows.Find(row))
Next
'Set DataGridView data source as the cleaned merged table
CRLForm.DataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect ' Otherwise DataSource = CRLTable refuses to populate
CRLForm.DataGridView1.DataSource = CRLTable
CRLForm.DataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
End Sub