zoukankan      html  css  js  c++  java
  • Ado ext for ddl and security (zz)

    1. //Reference Microsoft ADO Ext x.x for DDL and Security
    2. ADOX.Catalog cat = new ADOX.Catalog();
    3. ADOX.Table tbl = new ADOX.Table();
    4.  
    5.  
    6. String linkToDB = "Z:\\Docs\\Test.accdb";
    7. String linkInDB = "Z:\\Docs\\Test2.accdb";
    8.  
    9. String cn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + linkInDB;
    10.  
    11. cat.let_ActiveConnection(cn);
    12.  
    13. tbl.ParentCatalog = cat;
    14. tbl.Name = "LinkTableDB";
    15. tbl.Properties["Jet OLEDB:Remote Table Name"].Value  ="Table1";
    16. tbl.Properties["Jet OLEDB:Link Datasource"].Value = linkToDB;
    17. tbl.Properties["Jet OLEDB:Link Provider String"].Value ="MS Access";
    18. tbl.Properties["Jet OLEDB:Create Link"].Value =true;
    19.  
    20. cat.Tables.Append(tbl);
    //z 2012-08-16 16:26:24 IS2120@CSDN.T2815507050[T7,L256,R11,V329]
    Sub CreateLinkedJetTable()
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    
    Set cat = New ADOX.Catalog
    
    ' Open the catalog.
    cat.ActiveConnection = CurrentProject.Connection
    
    Set tbl = New ADOX.Table
    
    ' Create the new table.
    tbl.Name = "Linked_Employees"
    Set tbl.ParentCatalog = cat
    
    ' Set the properties to create the link.
    tbl.Properties("Jet OLEDB:Link Datasource") = "C:\Program Files\Microsoft Office\Office\Samples\northwind.mdb"
    tbl.Properties("Jet OLEDB:Remote Table Name") = "Employees"
    tbl.Properties("Jet OLEDB:Create Link") = True
    
    ' To link a table with a database password set the Link Provider String
    ' tbl.Properties("Jet OLEDB:Link Provider String") = "MS Access;PWD=Admin;"
    
    ' Append the table to the tables collection.
    cat.Tables.Append tbl
    Set cat = Nothing
    
    End Sub
    
    Sub RefreshLinks()
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    
    Set cat = New ADOX.Catalog
    
    ' Open the catalog.
    cat.ActiveConnection = CurrentProject.Connection
    
    Set tbl = New ADOX.Table
    
    For Each tbl In cat.Tables
    ' Verify that the table is a linked table.
        If tbl.Type = "LINK" Then
            tbl.Properties("Jet OLEDB:Link Datasource") = "C:\Program Files\Microsoft Office\Office\Samples\northwind.mdb"
    ' To refresh a linked table with a database password set the Link Provider String
    'tbl.Properties("Jet OLEDB:Link Provider String") = "MS Access;PWD=Admin;"
        End If
    Next
    End Sub
    		

    For one of my contracts I decided to split the MS Access into two database one to collect the data and one for reporting. The reporting database would linked in all the tables from the data database. This way I can modify the reports email the client the report database and do not need to port any data over to the newer database or provide scripts to update the database.

    The problem is that the linked table source database is full hardcoded path. To keep it easy for the clients I needed a way to have the program updated the linked table source database path from the program.

    I googled around but was not able to find a solution but did find some leads which led to the following code snippet. The code snippet reset the linked tables source database path.

    1. ADODB.Connection Con = new ADODB.Connection();  
    2. ADOX.Catalog Cat = new ADOX.Catalog();  
    3.   
    4. Con.Open(connectionString, nullnull, 0);  
    5. Cat.ActiveConnection = Con;  
    6. Cat.Tables[LinkedTableName].Properties["Jet OLEDB:Link Datasource"].Value = LinkedDatabaseLocation;  
    7. Con.Close(); 
    //z 2012-08-16 16:26:24 IS2120@CSDN.T2815507050[T7,L256,R11,V329]

    VBA
    1. 'Reference Microsoft ADO Ext x.x for DDL and Security
    2.     Dim cn 'As ADODB.Connection
    3.     Dim ct 'As ADOX.Catalog
    4.     Dim tbl 'As ADOX.Table
    5.    
    6.     Dim strLinkXL 'As String
    7.     Dim strLinkMDB 'As String
    8.     Dim strMDB 'As String
    9.    
    10.     strLinkXL = "C:\Docs\LTD.xls"
    11.     strLinkMDB = "C:\Docs\db1.mdb"
    12.     strMDB = "C:\Docs\LTD.mdb"
    13.    
    14.     'Create Link...
    15.     Set cn = CreateObject("ADODB.Connection")
    16.     cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    17.            "Data Source=" & strMDB & ";" & _
    18.            "Persist Security Info=False"
    19.    
    20.     Set ct = CreateObject("ADOX.Catalog")
    21.     Set ct.ActiveConnection = cn
    22.    
    23.     Set tbl = CreateObject("ADOX.Table")
    24.     Set tbl.ParentCatalog = ct
    25.    
    26.     '1. Link MDB
    27.    
    28.     With tbl
    29.       'What the link table will be called
    30.       .Name = "LinkTableMDB"
    31.       'Name of the table to link
    32.       .properties("Jet OLEDB:Remote Table Name") = "Table1"
    33.       .properties("Jet OLEDB:Link Datasource") = strLinkMDB
    34.       .properties("Jet OLEDB:Link Provider String") = "MS Access"
    35.       .properties("Jet OLEDB:Create Link") = True
    36.     End With
    37.    
    38.     'Append the table to the tables collection
    39.     ct.Tables.Append tbl
    40.     Set tbl = Nothing
    41.    
    42.     '2. Link Excel using named range
    43.     Set tbl = CreateObject("ADOX.Table")
    44.     Set tbl.ParentCatalog = ct
    45.    
    46.     With tbl
    47.       .Name = "LinkTableXLRange"
    48.       .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" & strLinkXL & ";HDR=Yes"
    49.       'The named range
    50.       .properties("Jet OLEDB:Remote Table Name") = "Data_Range"
    51.       .properties("Jet OLEDB:Create Link") = True
    52.     End With
    53.    
    54.     'Append the table to the tables collection
    55.     ct.Tables.Append tbl
    56.     Set tbl = Nothing
    57.    
    58.     '3. Link Excel by sheet name
    59.     Set tbl = CreateObject("ADOX.Table")
    60.     Set tbl.ParentCatalog = ct
    61.    
    62.     With tbl
    63.       .Name = "LinkTableXLSheet"
    64.       .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" & strLinkXL & ";HDR=Yes"
    65.       'Note the use of $, it is necessary
    66.       .properties("Jet OLEDB:Remote Table Name") = "Sheet2$"
    67.       .properties("Jet OLEDB:Create Link") = True
    68.     End With
    69.    
    70.     'Append the table to the tables collection
    71.     ct.Tables.Append tbl
    72.     Set tbl = Nothing

  • 相关阅读:
    32位和64位系统区别及int字节数
    c语言指针占几个字节
    可重入和不可重入
    C中的volatile用法
    让你分分钟读懂CPU架构及芯片厂商
    手机CPU知识扫盲:谈谈手机CPU架构与原理 (全
    IO端口、IO内存、IO空间、内存空间的含义和联系
    IO端口和IO内存的区别及分别使用的函数接口
    linux终端下 编译c语言程序
    git各种撤销操作
  • 原文地址:https://www.cnblogs.com/IS2120/p/6745848.html
Copyright © 2011-2022 走看看