Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server EXCEL VBA到SQL索引和查找_Sql Server_Vba_Excel - Fatal编程技术网

Sql server EXCEL VBA到SQL索引和查找

Sql server EXCEL VBA到SQL索引和查找,sql-server,vba,excel,Sql Server,Vba,Excel,我正在将Excel表中的数据导出到SQL Server数据库,如果存在,请更新或插入 以下VBA代码适用于导出到ACCESS数据库, 但不是到SQL SERVER数据库表 出现错误消息:对.Index和.Seek的属性使用无效 请帮忙!!!托赫 Sub ExcelDataToSql () Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long Dim lastrow As Long, o As Long Set cn = Ne

我正在将Excel表中的数据导出到SQL Server数据库,如果存在,请更新或插入

以下VBA代码适用于导出到ACCESS数据库, 但不是到SQL SERVER数据库表

出现错误消息:对.Index和.Seek的属性使用无效

请帮忙!!!托赫

Sub ExcelDataToSql ()

Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim lastrow As Long, o As Long

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.Open "Provider=SQLNCLI11;Server=***;Database=****;Trusted_Connection=yes;"
rs.CursorLocation = adUseServer
rs.Open "InventorySQL", cn, 1, 3, adCmdTableDirect

' Get Lastrow
Worksheets("InventoryEXCEL").Select
lastrow = Worksheets("InventoryEXCEL").Cells(rows.Count, 1).End(xlUp).Row
r = 2 ' the start row in the worksheet
For o = 2 To lastrow

    'Check For Duplicate In Database SQL
    With rs
        .Index = "PrimaryKey"
        .Seek Range("A" & r).Value

        If .EOF Then
            .AddNew            
            'If No Duplicate insert New Record
            rs.Fields("oPartno") = Range("A" & r).Value
            rs.Fields("oDesc") = Range("B" & r).Value
            rs.Fields("oCost") = Range("C" & r).Value
        .update
        Else          
            ' If Duplicate Found Update Existing Record
            rs.Fields("oDesc") = Range("B" & r).Value
            rs.Fields("oCost") = Range("C & r).Value
            .Update
        End If
    End With

Next o
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    MsgBox "Posting Completed"
End Sub

。Index=“PrimaryKey”--Sysntax错误:属性的使用无效 .Seek范围(“A”&r).值系统NTAX错误:

参考:

MSDN示例将数组作为第一个参数传递

rstEmployees.Seek Array(strID), adSeekFirstEQ
第一个参数的名称os
KeyValues
,这也意味着一个数组

我会先试试这个

.Seek数组(范围(“A”&r).Value)

使用
SeekEnum
值之一也可能有益

更新:操作人员发现这是相关的代码片段

MSDN还建议检查提供商是否支持
.Index
.Seek

If rstEmployees.Supports(adIndex) And rstEmployees.Supports(adSeek) Then

我的问题通过变通解决了

许多参考资料表明,Sql提供程序不支持索引和查找功能。所以我避免索引和查找

我通过将excel工作表作为源表导入Sql server来解决此问题。。。然后将目标表与源表合并。。。如果匹配则更新,如果不匹配则插入

select * from InventoryTableSQL
select * from InventoryTableFromExcel

Merge InventoryTableSQL as T
using InventoryTableFromExcel as S
on t.oPartno = s.oPartno
when matched then
    update set t.oPartno = s.oPartno,
    t.oDesc = s.oDesc,
    t.oCost = s.oCost
when not matched by target then
    insert(oPartno, oDesc, oCost) values(s.oPartno, s.oDesc, s.oCost));

为什么r和mysal被标记在这里?如果我没有误解的话,SQL Server似乎是一个更合适的标签?谢谢您的反馈。。。这是我在stackoverflow的第一篇帖子…以后会更加小心。。。对不起,谢谢你的帮助。提供程序似乎不支持.index和.seek属性和方法。感谢更新。我修改了我的答案。试试你的方法。搜索数组(范围(“A”和“r”).valuye,seekEnum…运行时错误3251-当前提供程序不支持索引功能所需的接口。@您知道吗?他们有一个完整的提供程序列表。是的…我使用ConnectionStrins.Com中的提供程序。如何知道哪个提供程序为.index和.seek提供功能。连接当前使用时没有连接问题。