c# - 使用 powershell 反射执行数据库更新

标签 c# powershell

我正在编辑我的问题以使其更易于理解。

这是一个示例解决方案:https://github.com/mckenn55/PowershellTest

我创建了一个全新的 net47 MVC 项目。我在该区域内添加了一个名为“数据库”的区域和一个名为“更新”的 Controller 。在该 Controller 中,我有以下内容:

    public ActionResult Index()
    {
        return View(Execute());
    }

    public static List<string> Execute()
    {
        var returnable = new List<string>();

        var assembly = Assembly.GetExecutingAssembly();

        string codeBase = assembly.CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        var assemblyLocation = Path.GetDirectoryName(path);

        string resourcePath = "";

        string ModuleName = assembly.ManifestModule.Name;
        ModuleName = ModuleName.Substring(0, ModuleName.LastIndexOf("."));
        ModuleName = ModuleName.Replace(' ', '_').Replace(".", "");

        string FolderPath = "Areas.Database.SQL";
        FolderPath = FolderPath.Replace(' ', '_');

        if (FolderPath != null && FolderPath.Length > 0 && FolderPath[FolderPath.Length - 1] == '.')
            FolderPath = FolderPath.Substring(0, FolderPath.Length - 1);

        StringBuilder filepath = new StringBuilder();
        filepath.Append(ModuleName);
        if (FolderPath != null && FolderPath.Length > 0)
        {
            filepath.Append('.' + FolderPath);
            filepath.Append('.');
        }
        resourcePath = filepath.ToString();

        string[] resourceNames = assembly.GetManifestResourceNames();
        foreach (var resourceName in resourceNames)
        {
            if (Regex.Match(resourceName, "^" + resourcePath).Success)
            {
                returnable.Add(resourceName);
            }
        }

        var orderedFileNames = new List<string>();
        if (returnable != null && returnable.Any())
        {
            orderedFileNames = returnable.OrderBy(q => q).ToList();
        }
        else
        {
            returnable.Add("No files found");
        }

        return returnable;
    }

在数据库区域中,我有一个名为“SQL”的目录,在该目录中,我有一个文件 TestFile.sql,作为嵌入式资源包含在解决方案中。使用索引操作查看时,Execute() 方法的结果是“PSTest.Areas.Database.SQL.TestFile.sql”。我希望在 Powershell 中看到同样的事情。我尝试了以下方法:

> Add-Type -path "C:\Temp\PSTest\PSTest\bin\PSTest.dll"
> [PSTest.Areas.Database.UpdateController]::Execute()
No Files Found

我的目标是否可以通过 powershell 实现?如果可以,如何实现?

最佳答案

我无法单独使用 powershell 找到解决方案。我编写了一个 C# 控制台应用程序,它能够毫无问题地执行这些操作。

关于c# - 使用 powershell 反射执行数据库更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49700834/

相关文章:

c# - 如何在 C# 中导航任何 JSON 树?

powershell - 将参数传递给 Powershell 调用运算符 - 我无法让它工作

c# - 如何从 WPF 中的基类型集合中为子类型集合自动生成 DataGrid 列?

c# - 返回变量是由编译器自动创建的吗?

sql-server - 尽管安装了 SQL Server,但未安装 SQL Server PowerShell 管理单元

shell - 如何在powershell中运行cygwin脚本

c# - Windows 服务 : Get processes for all users, 包括 MainWindowHandle

c# - 使用 C# .NET Core 3.1 返回具有属性的组成员的 Active Directory

javascript - AJAX 调用带有参数的函数

c# - CloudTable 异步执行查询