【问题标题】:Is it possible to fill a data table from another data table by SQL QUERY in VB.net是否可以通过 VB.net 中的 SQL QUERY 从另一个数据表中填充数据表
【发布时间】:2014-04-14 12:28:48
【问题描述】:

我有一个使用oledb 从 MS_Excel 填充的数据表 (dtExcelSource)。 现在我有了第二个数据表,它是结构化的 (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 列,所以想将每一行插入到第二个数据表中。

【问题讨论】:

    标签: sql vb.net datatable


    【解决方案1】:

    你的第二张桌子和你的第一张桌子的结构一样吗?
    当你说

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

    您的意思是只想从 Source 表中提取某些行以复制到您的第二个表中还是仅提取某些列?

    这里有一些东西可能会有所帮助,但就像我说的那样,你的帖子让我有点困惑。

       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
    

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2019-12-27
      • 2018-01-12
      • 2013-08-02
      • 2013-07-02
      • 1970-01-01
      相关资源
      最近更新 更多