3

我有一个 Access DB,我想从中提取源代码,以便将其放入源代码控制中。

我尝试使用主互操作程序集(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。谢谢。

4

4 回答 4

1

您可以使用未记录的Application.SaveAsTextApplication.LoadFromText函数。SaveAsText 适用于模块、表单和报告;当您将表单或报告另存为文本时,其模块代码将出现在生成的文本文件的底部。

您可以编写一个例程,将 Access MDB(或 ADP)中的所有非数据对象保存为文本,将其放入模块中,然后将该模块保留在 Access DB 的开发版本中。然后您可以运行例程并检查 VSS 中的转储代码。

它可能不如 Mitch Wheat 描述的 Visual SourceSafe 方法那么优雅,但这取决于您想对源代码做什么。我倾向于使用 MDB 的多个版本,并使用这种方法使用差异工具(例如 WinMerge)比较它们之间的源代码。它有利于在开发分支之间移植功能。

它还可以很好地定位对字段或控件的所有引用,无论它们在哪里。将 Access 对象视为文本定义使得查找这些引用变得非常简单。

于 2008-12-22T09:01:51.910 回答
1

有个更好的方法。您可以使用 Visual Sourcesafe(可能还有其他 SCC)来对代码和对象进行版本控制:请参阅此MSDN 文章

于 2008-10-13T14:44:13.597 回答
1

这可能会有所帮助:

    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

于 2008-10-13T14:57:42.290 回答
0

您可能还想看看他的问答:

在 MS Access 上与多个程序员合作

于 2008-11-03T05:30:44.020 回答