在我的PSCmdlet
课堂上,我要求用户使用PromptForChoice
. 提示用户选择他想要使用的连接字符串。我无法给出ChoiceDescriptions
正确的格式。
choiceDescriptions.Add(new ChoiceDescription("&"+i+externalConfig.ConnectionStrings.ConnectionStrings[i].ConnectionString));
给我这个
连接字符串
选择您要使用的连接字符串
[0] 0数据源=.\SQLEXPRESS;集成安全=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;用户实例=true
[1] 1有效的连接字符串;
[2] 2Data Source=111.111.1.1;初始目录=Test;User id=User;Password=Password;MultipleActiveResultSets=true
[?] 帮助(默认为“0”):
如您所见,这些数字也在连接字符串文本中,这不是我想要的。
这就是我想要实现的目标:
[0] 数据源=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;用户实例=true
等等等等
因此,我需要将用户选择为int
. 我怎样才能得到我想要达到的结果?
代码:
[Cmdlet(VerbsCommon.Get, "DemoNames")]
public class Get_DemoNames : PSCmdlet
{
[Parameter(Position = 0, Mandatory = false)]
public string prefix;
System.Diagnostics.Debugger.Launch();
var environment = Package.GetGlobalService(typeof(DTE)) as DTE2;
if (environment == null)
{
throw new Exception("Need a DTE");
}
var solution = new DirectoryInfo(environment.Solution.FullName);
var directory = string.Empty;
if (solution.Parent != null)
{
directory = Path.Combine(solution.Parent.FullName, @"Datamodel\bin\Debug");
}
else
{
throw new Exception("No directory for DLL-files");
}
var whereBuilder = new WhereImporter(directory);
EfXmlDependencyInjectionContainer.Wheres = whereBuilder.AllEfXmlWheres();
// get propertyinfo from database-contexts
var contextBuilder = new EfXmlContextPropertyBuilder(directory);
EfXmlDependencyInjectionContainer.ContextProperties = contextBuilder.GetPropertyInfo();
//get datamodels
var datamodelBuilder = new EfXmlDatamodelBuilder(directory);
/* Get the app/web config file */
var configFile = string.Empty;
var projects = environment.Solution.Projects.DTE.ActiveSolutionProjects;
foreach (var project in projects)
{
foreach (EnvDTE.ProjectItem item in project.ProjectItems)
{
if (Regex.IsMatch(item.Name, "(app|web).config", RegexOptions.IgnoreCase))
{
configFile = item.FileNames[0];
break;
}
}
}
/* Open config file */
var configMap = new ExeConfigurationFileMap
{
ExeConfigFilename = configFile
};
var externalConfig = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configMap,
ConfigurationUserLevel.None);
var conString = string.Empty;
/* No connectionstrings are found */
if (externalConfig.ConnectionStrings.ConnectionStrings.Count == 0)
{
throw new Exception($"No connectionstrings found in: {configFile}");
}
/* If there's only one connectionstring, pick that one. */
if (externalConfig.ConnectionStrings.ConnectionStrings.Count == 1)
{
conString = externalConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
}
/* Give the user the choice to choose the connectionstring. */
if (externalConfig.ConnectionStrings.ConnectionStrings.Count > 1)
{
var choiceDescriptions = new Collection<ChoiceDescription>();
for (int i = 0; i < externalConfig.ConnectionStrings.ConnectionStrings.Count; i++)
{
choiceDescriptions.Add(new ChoiceDescription("&"+i+externalConfig.ConnectionStrings.ConnectionStrings[i].ConnectionString));
}
var choice = this.Host.UI.PromptForChoice("Connection String", "Choose the connection string you want to use",
choiceDescriptions, 0);
conString = externalConfig.ConnectionStrings.ConnectionStrings[choice].ConnectionString;
WriteObject(choice);
}
var parser = new Parser
{
CurrentProjectDirectory = solution.Parent.FullName,
DbConnectionString = conString
};
parser.UpdateXml(datamodelBuilder);
}
}