简体   繁体   English

从MS Access DB中提取源代码

[英]Extracting Source Code from an MS Access DB

I have an Access DB that I would like to extract the source code from so I can put it into Source control. 我有一个Access DB,我想从中提取源代码,所以我可以把它放到Source控件中。

I have tried to extract the data using the Primary Interop Assemblies(PIA), but I am getting issues as it is not picking up all of the modules and forms. 我试图使用主互操作程序集(PIA)提取数据,但我遇到了问题,因为它没有获取所有模块和表单。

There are 140 Forms and Modules in the code(Don't ask, it's a legacy system I have inherited) but the PIA code is only picking up 91 of them. 代码中有140个表单和模块(不要问,它是我继承的遗留系统),但PIA代码只获取了91个。

Here is the code I am using. 这是我正在使用的代码。

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);
            }

        }
    }
}

Any suggestions on what that code might be missing? 有关该代码可能缺失的任何建议? or on a product/tool out there that will do this for me? 或者在那里为我做这件事的产品/工具?

Edit: I should also mention that this needs to script to disk, integration with VSS is not an option as our source system is SVN. 编辑:我还应该提到这需要脚本到磁盘,与VSS的集成不是一个选项,因为我们的源系统是SVN。 Thanks. 谢谢。

There is a better way. 有一个更好的办法。 You can use Visual Sourcesafe (and possibly other SCCs) to version control code and objects in place: see this MSDN article 您可以使用Visual Sourcesafe(可能还有其他SCC)来控制代码和对象的版本:请参阅此MSDN文章

This may help: 这可能有所帮助:

    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  

From: http://wiki.lessthandot.com/index.php/Code_and_Code_Windows 来自: http//wiki.lessthandot.com/index.php/Code_and_Code_Windows

You could use the undocumented Application.SaveAsText and Application.LoadFromText functions. 您可以使用未记录的Application.SaveAsTextApplication.LoadFromText函数。 SaveAsText works on modules, forms, and reports; SaveAsText适用于模块,表单和报表; and when you save a form or report as text, its module code will appear at the bottom of the resulting text file. 当您将表单或报表另存为文本时,其模块代码将显示在生成的文本文件的底部。

You could write a routine that would save all of the non-data objects in your Access MDB (or ADP) as text, put it in a module, and just keep that module in a development version of your Access DB. 您可以编写一个例程,将Access MDB(或ADP)中的所有非数据对象保存为文本,将其放在模块中,并将该模块保留在Access DB的开发版本中。 Then you could run the routine and check in the dumped code in VSS. 然后,您可以运行例程并检入VSS中的转储代码。

It's probably not as elegant as the Visual SourceSafe method described by Mitch Wheat, but that depends on what you want to do with the source code. 它可能不如Mitch Wheat描述的Visual SourceSafe方法那么优雅,但这取决于你想要对源代码做什么。 I tend to hang onto multiple versions of MDB's, and use this method to compare source code between them using diff tools such as WinMerge. 我倾向于挂在MDB的多个版本上,并使用此方法使用诸如WinMerge之类的diff工具来比较它们之间的源代码。 It's good for porting functionality between branches of development. 在开发分支之间移植功能很有用。

It's also good for locating all references to fields or controls wherever they may be found. 它也适用于查找字段或控件的所有引用,无论它们位于何处。 Viewing Access objects as textual definitions makes finding these references dead simple. 将Access对象视为文本定义使得查找这些引用变得简单。

You might also want to take a look at his Q&A: 您可能还想看看他的问答:

Working with multiple programmers on MS Access 在MS Access上与多个程序员一起工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM