从 MS Access DB 中提取源代码

发布于 2024-07-06 10:30:27 字数 1614 浏览 6 评论 0原文

我有一个 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。 谢谢。

I have an Access DB that I would like to extract the source code from so I can put it into Source control.

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.

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.

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. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-07-13 10:30:27

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

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

不及他 2024-07-13 10:30:27

这可能会有所帮助:

    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< /a>

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

难得心□动 2024-07-13 10:30:27

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

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

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

它还有助于在任何地方找到对字段或控件的所有引用。 将 Access 对象视为文本定义使得查找这些引用变得非常简单。

You could use the undocumented Application.SaveAsText and Application.LoadFromText functions. SaveAsText works on modules, forms, and reports; 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. Then you could run the routine and check in the dumped code in 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. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文