是否可以通过VB.net中的SQL QUERY从另一个数据表中填充数据表

时间:2014-04-14 12:28:49

标签: sql vb.net datatable

我有一个数据表(dtExcelSource),它使用oledb从MS_Excel填充。 现在我有了第二个数据表,它是结构化的(SqlDbType.Structured)。

我的要求是,使用查询或任何其他方法用数据表(dtExcelSource)中的少量值填充第二个数据表。

 MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=""Excel 12.0 Xml;HDR=NO"";")
            MyConnection.Open()
            dtExcelSchema = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
            MyCommand = New OleDb.OleDbDataAdapter("Select * from [" & SheetName & "A5:F85]", MyConnection)
            DS = New System.Data.DataSet()
            MyCommand.Fill(DS, "SourceTbl")
            dtExcelSource = DS.Tables("SourceTbl")
            'dtExcelSource.Columns(0).DataType = GetType(Integer)
            dtExcelSource.Columns(0).ColumnName = "Serial"
            dtExcelSource.Columns(1).ColumnName = "Document A"
            dtExcelSource.Columns(2).ColumnName = "Contract A"
            dtExcelSource.Columns(3).ColumnName = "Subscriber A"
            dtExcelSource.Columns(4).ColumnName = "Document B"
       Return dtExcelSource

我的第二个数据表有15列,所以想要将每一行插入第二个数据表。

1 个答案:

答案 0 :(得分:1)

你的第二张桌子与第一张桌子的结构是否相同? 当你说

  

我的要求是,用数据表(dtExcelSource)中的几个值填充第二个数据表

您是不是想从源表中仅提取某些行来复制到第二个表或仅删除某些列?

这里有一点可以看,这可能会有所帮助,但就像我说你的帖子对我来说有点混乱。

   Private Sub FillSecondDatatable()
            Dim dtExcelSource As New DataTable
            Dim dtSecondTable As New DataTable
            ' Put your code that populates dtExcelSource  in a function that returns the datatable
            dtExcelSource = SomeFunctionCallToYourCode()
            'If something comes back then act on it
            If dtExcelSource IsNot Nothing AndAlso dtExcelSource.Rows.Count > 0 Then
                    'I'm cloning your dtEscelSource structure into my
                    'new table because I couldn't understand your 
                    'post completely
                    dtSecondTable = dtExcelSource.Clone
                    'The Select procedure for datatables returns
                    'an array of datarows 
                    Dim arReturnedRows() As DataRow = Nothing
                    'Call Select to filter down to which rows you want
                    arReturnedRows = dtExcelSource.Select("Serial = SomeSerialValue")
                    If arReturnedRows IsNot Nothing AndAlso arReturnedRows.Count > 0 Then
                            'Spin through the returned rows and
                            'import the row into the Second table.
                            '1 row cannot belong to multiple tables so you
                            'can't just add it to the second table while
                            'it belongs to the first table
                            For Each rw As DataRow In arReturnedRows
                                    dtSecondTable.ImportRow(rw)
                            Next
                    End If
            End If
    End Sub