在VB.net中,是否可以通过SQL查询从另一个数据表填充数据表

在VB.net中,是否可以通过SQL查询从另一个数据表填充数据表,sql,vb.net,datatable,Sql,Vb.net,Datatable,我有一个数据表(dtExcelSource),它是使用oledb从MS_Excel中填充的。 现在我有了第二个数据表,它是结构化的(SqlDbType.structured) 我的要求是,使用查询或任何其他方法,用数据表(dtExcelSource)中的少量值填充第二个数据表 MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath &

我有一个数据表(
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列,所以希望将每行插入第二个数据表。

第二个表的结构是否与第一个表相同?
当你说

我的要求是,用数据表(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