我需要通过程序在 SQL Server 上启用 FILESTREAM。根据我找到的文档,这可以通过 WMI 使用以下代码完成:
set fsInstance = GetObject("WINMGMTS:\\" & MachineName & "\root\Microsoft\SqlServer\ComputerManagement10:FilestreamSettings='" & InstanceName & "'")
Set method = fsInstance.Methods_("EnableFilestream")
Set inParam = method.inParameters.SpawnInstance_()
inParam.AccessLevel = TargetLevel
inParam.ShareName = ShareName
Set outParam = fsInstance.ExecMethod_("EnableFilestream", inParam)
将其转换为 C#,我得到以下信息:
ManagementClass mc = new ManagementClass(new ManagementScope(@"\\.\root\Microsoft\SqlServer\ComputerManagement10"),
new ManagementPath("FilestreamSettings"),
new ObjectGetOptions(new ManagementNamedValueCollection() {
{"InstanceName", "MSSQLSERVER"}
}));
ManagementBaseObject inParams = mc.GetMethodParameters("EnableFilestream");
inParams["AccessLevel"] = 1;
inParams["ShareName"] = "ALLIANCE";
mc.InvokeMethod("EnableFilestream", inParams, null);
但是,每当我尝试这样做时,它都会在 InvokeMethod 上引发异常。消息是“无效的方法参数”。我试图删除设置 AccessLevel 和 ShareName 的两行并以这种方式运行,但得到相同的异常。我在这里做错了什么?