2013-09-21 117 views
0

我想通過使用MS Excel宏的幫助將數據插入到創建「test.SDF」數據庫文件表的SQLCE中。我不想將我的Excel文件轉換爲.SDF,但想要使用宏從我的Excel文件插入數據到SDF。使用宏連接到SQL精簡版

到現在我已經做到了這一點

Dim strconn As String 
    Dim cnt As ADODB.Connection 
    Dim rst As ADODB.Recordset 
    Dim cmd As ADODB.Command 


    strconn = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5; Data Source= H:\Excel files and Macros\TechData.sdf;Persist Security Info=False;" 
    Set cmd = New ADODB.Command 
    Set cnt = New ADODB.Connection 
    cnt.ConnectionString = strconn 
    cnt.Open 
    Runsql ("Insert into UnitData Values(" & Version & ", " & Acoustic & ")") 

    cnt.Close 

我對着下面的錯誤「錯誤3706:無法執行提供商,它可能不是 正確安裝。」

在行「cnt.Open」 我還添加了引用「微軟的ActiveX」數據對象2.0庫

感謝您的幫助和有關問題的任何進一步的間隙請評論。

+0

你試過了什麼?谷歌有很多這方面的例子 - 你是否嘗試過使用其中之一? –

+0

我試過了下面的代碼。 – Rahul

+0

您是否安裝了SQLCE 3.5 SP2 MSI? – ErikEJ

回答

0

好的,請用下面的代碼代替它,它會記住您的系統中安裝了SQLCE 3.5。

Dim stConnection As String 
    Dim ADODBcnt As New ADODB.Connection 

    Dim dbRecord As New ADODB.Recordset 
    dbRecord.CursorLocation = adUseServer 
    dbRecord.CursorType = adOpenKeyset 
    dbRecord.LockType = adLockBatchOptimistic 

    stConnection = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5; Data Source= filepath\file.sdf;Persist Security Info=False" 
    'Instantiate the ADODB objects.' 
    If Not ADODBcnt.State Then 
     ADODBcnt.ConnectionString = stConnection 
     ADODBcnt.Open 
    End If 

C_stSQL = "Insert into UnitData Values('" & Version & "', '" & Acoustic & "');" 

    dbRecord.Open C_stSQL, ADODBcnt 
    If (dbRecord.State = 1) Then 
     dbRecord.Close 
    End If 

    ADODBcnt.Close