使用ADO将SQL Server Compact 3.5数据库连接到MS Access 2003?

时间:2008-11-29 20:39:08

标签: ms-access sql-server-ce

是否可以在MS Access 2003中编写连接并打开SQL Compact 3.5数据库?我希望能够使用MS Access 2003来操作SQL Compact 3.5数据库中的数据。如果可能,那么将使用哪些语句来打开数据库?

2 个答案:

答案 0 :(得分:2)

这只是一个想法,我无法确认它是否可行,但鉴于SQL Compact缺少ODBC驱动程序并且您不能拥有链接表,也许您可​​以使用SQL Compact的OLEDB连接字符串作为Source Connect中保存的QueryDef的源连接字符串。如果你可以让它工作,你可以为每个表创建一个保存的QueryDef,然后使用它们,就像查询是链接表一样。

我无法在我的机器上测试它,因为我安装的唯一OLEDB提供程序是Jet,而Access似乎不喜欢它。

但值得一试。可能它不会起作用,因为我找不到任何人在Access中做过这个的任何地方。但我真的不明白为什么它不起作用。

然而,我可能只是错了。

答案 1 :(得分:1)

虽然我没有专门使用SQL Compact,但连接服务器应该是标准的:

  1. 检查可用工具中是否正确引用了ADODB文件(msado21.tlb)
  2. 写下你的连接字符串,如下所示:

    MyConnectionString =“Provider = SQLOLEDB.1; Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = YourDatabaseName; Data Source = YourSQLServerInstanceName”

    此字符串是为“集成安全性”上下文编写的。在cas中,您希望为SQL安全上下文更改它,请检查here以更新字符串。理想情况下,此字符串应在代码中声明为公共变量。

  3. 完成此操作后,您可以打开ADODB记录集并开始操作它:

    public sub connectionTest
    Dim activeConnection as ADODB.connection, _
        activeRecordset as ADODB.recordset
    
    Set activeConnection = New ADODB.connection
    activeConnection.connectionString = myCOnnectionString
    activeConnection.open
    
    set activeRecordset = New ADODB.recordset
    'this will open a read-only recordset'
    activeRecordset.open _
        "SELECT * FROM myTableName", _
        activeConnection, _
        adOpenStatic, _
        adLockReadOnly
    
    if activeRecordset.EOF and activeRecordset.BOF then
        debug.print "No records in this table"
    else
        activeRecordset.moveFirst
        do while not activeRecordset.EOF
            debug.print activerecordset.fields("myFieldName").value
            activeRecordset.moveNext
        loop
    endif
    
    activeRecordset.close
    set activeRecordset = nothing
    activeConnection.close
    set activeConnection = nothing
    
    end sub