簡體   English   中英

從MS Access DB中提取源代碼

[英]Extracting Source Code from an MS Access DB

我有一個Access DB,我想從中提取源代碼,所以我可以把它放到Source控件中。

我試圖使用主互操作程序集(PIA)提取數據,但我遇到了問題,因為它沒有獲取所有模塊和表單。

代碼中有140個表單和模塊(不要問,它是我繼承的遺留系統),但PIA代碼只獲取了91個。

這是我正在使用的代碼。

using System;
using Microsoft.Office.Interop.Access;

namespace GetAccesSourceFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            ApplicationClass appClass = new ApplicationClass();
            try
            {
                appClass.OpenCurrentDatabase("C:\\svn\\projects\\db.mdb",false,"");

                Console.WriteLine(appClass.Version);
                Console.WriteLine(appClass.Modules.Count.ToString());
                Console.WriteLine(appClass.Modules.Parent.ToString());

                int NumOfLines = 0;
                for (int i = 0; i < appClass.Modules.Count; i++)
                {
                    Console.WriteLine(appClass.Modules[i].Name + " : " + appClass.Modules[i].CountOfLines);
                    NumOfLines += appClass.Modules[i].CountOfLines;
                }

                Console.WriteLine("Number of Lines : " + NumOfLines);
                Console.ReadKey();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" +ex.StackTrace);
            }
            finally
            {
                appClass.CloseCurrentDatabase();
                appClass.Quit(AcQuitOption.acQuitSaveNone);
            }

        }
    }
}

有關該代碼可能缺失的任何建議? 或者在那里為我做這件事的產品/工具?

編輯:我還應該提到這需要腳本到磁盤,與VSS的集成不是一個選項,因為我們的源系統是SVN。 謝謝。

有一個更好的辦法。 您可以使用Visual Sourcesafe(可能還有其他SCC)來控制代碼和對象的版本:請參閱此MSDN文章

這可能有所幫助:

    Sub AllCodeToDesktop()
       'The reference for the FileSystemObject Object is Windows Script Host Object Model
       'but it not necessary to add the reference for this procedure.

       Dim fs As Object
       Dim f As Object
       Dim strMod As String
       Dim mdl As Object
       Dim i As Integer

       Set fs = CreateObject("Scripting.FileSystemObject")

       'Set up the file.
       Set f = fs.CreateTextFile(SpFolder("Desktop") & "\" _
         & Replace(CurrentProject.Name, ".", "") & ".txt")

       'For each component in the project ...
       For Each mdl In VBE.ActiveVBProject.VBComponents
           'using the count of lines ...
           i = VBE.ActiveVBProject.VBComponents(mdl.Name).CodeModule.CountOfLines
           'put the code in a string ...
           If VBE.ActiveVBProject.VBComponents(mdl.Name).codemodule.CountOfLines > 0 Then
              strMod = VBE.ActiveVBProject.VBComponents(mdl.Name).codemodule.Lines(1, i)
           End If
           'and then write it to a file, first marking the start with
           'some equal signs and the component name.
           f.writeline String(15, "=") & vbCrLf & mdl.Name _
               & vbCrLf & String(15, "=") & vbCrLf & strMod
       Next

       'Close eveything
       f.Close
       Set fs = Nothing
   End Sub

   Function SpFolder(SpName As String)
   'Special folders
       SpFolder = CreateObject("WScript.Shell").SpecialFolders(SpName)
   End Function  

來自: http//wiki.lessthandot.com/index.php/Code_and_Code_Windows

您可以使用未記錄的Application.SaveAsTextApplication.LoadFromText函數。 SaveAsText適用於模塊,表單和報表; 當您將表單或報表另存為文本時,其模塊代碼將顯示在生成的文本文件的底部。

您可以編寫一個例程,將Access MDB(或ADP)中的所有非數據對象保存為文本,將其放在模塊中,並將該模塊保留在Access DB的開發版本中。 然后,您可以運行例程並檢入VSS中的轉儲代碼。

它可能不如Mitch Wheat描述的Visual SourceSafe方法那么優雅,但這取決於你想要對源代碼做什么。 我傾向於掛在MDB的多個版本上,並使用此方法使用諸如WinMerge之類的diff工具來比較它們之間的源代碼。 在開發分支之間移植功能很有用。

它也適用於查找字段或控件的所有引用,無論它們位於何處。 將Access對象視為文本定義使得查找這些引用變得簡單。

您可能還想看看他的問答:

在MS Access上與多個程序員一起工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM