2016-08-25 51 views
1

我寫了一個小程序,它在輸入中獲取一個.xlsx文件(該文件是定期更新的),並將數據提取到一個sql表中。 我想通過比較excel文件的內容和sql表格的內容來定期更新這個sql表格,並且如果它們存在,將所有新行插入到它中。 我搜索了很多沒有成功的解決方案。 我該怎麼辦?使用vb.net從excel文件更新SQL表

我的實際代碼如下所示:

Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & SourceFile & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes""") 
     ExcelConnection.Open() 
     Dim RequeteExcelMat As String = "SELECT * FROM [feuil 1$]" 
     Dim objCmdSelect As OleDbCommand = New OleDbCommand(RequeteExcelMat, ExcelConnection) 
     Dim objDR As OleDbDataReader 

     Dim ConnexionBDDMat As New SqlConnection("SERVER=(local);DATABASE=MatStat;Trusted_Connection=True") 

     Try 
      ConnexionBDDMat.Open() 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 

     Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(ConnexionBDDMat) 
      bulkCopy.DestinationTableName = "dbo.Material" 

      'If SourceFile Content different from "Material" Sql Table Content Then 
      ' Update "Material" Table by inserting the new rows from SourceFile 
      'Else 
      ' MsgBox("Table is already updated") 
      'End If 

      Try 
       objDR = objCmdSelect.ExecuteReader 
       bulkCopy.WriteToServer(objDR) 
       objDR.Close() 
       ConnexionBDDMat.Close() 

      Catch ex As Exception 
       MsgBox(ex.ToString) 
      End Try 
     End Using 

回答

1

我用這個用的小桌子一起更新從Excel /插入數據到SQL Server

Sub merge() 
    Dim CurConn As ADODB.Connection 
    Dim rst As ADODB.Recordset 
    Dim SomeName As Worksheet 
    Dim Lastrow, Sql, n, i 

    Set SomeName = Sheets("SomeName") 

    Lastrow = SomeName.Range("B" & Rows.Count).End(xlUp).Row 

    For i = 2 To Lastrow 

     If i < Lastrow Then 
      Sql = Sql + "(" + CStr(SomeName.Range("A" & i).Value) + ", '" + SomeName.Range("B" & i).Value + "', '" + SomeName.Range("C" & i).Value + "', '" + SomeName.Range("D" & i).Value + "', '" + SomeName.Range("E" & i).Value + "', '" + SomeName.Range("F" & i).Value + "')," 
     Else 
      Sql = Sql + "(" + CStr(SomeName.Range("A" & i).Value) + ", '" + SomeName.Range("B" & i).Value + "', '" + SomeName.Range("C" & i).Value + "', '" + SomeName.Range("D" & i).Value + "', '" + SomeName.Range("E" & i).Value + "', '" + SomeName.Range("F" & i).Value + "')" 
     End If 

    Next i 

    Set CurConn = New ADODB.Connection 
    CurConn.Open "Provider=SQLNCLI11;Server=SERVER\INSTANCE;Database=DATABASENAME;Trusted_Connection=yes;" 
    CurConn.CommandTimeout = 180 
    CurConn.CursorLocation = adUseClient 

    Sql = "MERGE [dbo].[SomeName] as target " _ 
    & "USING (" _ 
    & " SELECT [Userid]" _ 
    & "   ,[Login]" _ 
    & "   ,[name]" _ 
    & "   ,[org]" _ 
    & "   ,[pos]" _ 
    & "   ,[mail]" _ 
    & " FROM (VALUES" _ 
    & Sql _ 
    & " ) as t([Userid],[Login],[name],[org],[pos],[mail])" _ 
    & ") as source " _ 
    & "ON target.[Userid] =source.[Userid] AND target.[Login] =source.[Login] " _ 
    & "WHEN MATCHED THEN " _ 
    & " UPDATE SET target.[name] =source.[name], " _ 
    & "    target.[org] =source.[org], " _ 
    & "    target.[pos] =source.[pos], " _ 
    & "    target.[mail] = Source.[mail] " _ 
    & "WHEN NOT MATCHED THEN " _ 
    & " INSERT ([Userid],[Login],[name],[org],[pos],[mail]) " _ 
    & " VALUES (source.[Userid],source.[Login],source.[name],source.[org],source.[pos],source.[mail]); " 

    MsgBox ("Success!") 
    CurConn.Execute (Sql) 

    CurConn.Close 
    Set rst = Nothing 
    Set CurConn = Nothing 
End Sub 

很是怪異到Excel表格轉換爲VALUES (1,2,3,4,5,6),(7,8,9,10,11,12)..etc但它工作正常。你可以採用這個解決方案和f.e.插入一些臨時表,然後像上面顯示的那樣合併它。

1

一個名爲MaterialStaging相同的模式創建一個表。批量插入此表格而不是常規表格。

然後,運行使用MERGE命令的存儲過程來查找生產表與暫存表之間的差異,並更新或插入差異。