如何在clickonce安装程序中请求连接字符串?

时间:2013-06-04 12:01:24

标签: c# connection-string clickonce

是否有配置clickonce所以它会提示用户填写应用程序的连接字符串?

就像,我将为许多用户部署我的应用程序,并且在每种情况下,sql server都将获得一个实例名称,可能不同,并且主机肯定会是不同的。那么如何配置它以安装正确的连接字符串?

1 个答案:

答案 0 :(得分:0)

这里有两种方法 - 当你开始应用程序检查时,是否在应用程序设置中显示了连接字符串;如果没有要求用户给你一个(确保将应用程序设置存储在隔离存储中)。

如果你想自动,并且你在网络服务器上托管你的应用程序,你可以将连接字符串作为激活网址的一部分传递(而不是/ClickOnce.application直接用户到页面/ClickOnce.application?ConnectionString=My_Encoded_ConnectionString )从应用程序本身你可以像这样阅读它

  try {
    if (ApplicationDeployment.CurrentDeployment == null ||
      ApplicationDeployment.CurrentDeployment.ActivationUri == null)
      return null;
  } catch {
    // application was not activated from the web
    return null;
  }

  Uri uri = ApplicationDeployment.CurrentDeployment.ActivationUri;
  string query = uri.Query;
  if (string.IsNullOrEmpty(uri.Query))
    throw new UIException("Activation Uri is empty.");

  Regex regex;
  Match match;
  regex = new Regex(@".*ConnectionString=(?<ConnectionString>([A-Z0-9\+/=:%\._-]+))", RegexOptions.IgnoreCase);
  match = regex.Match(uri.Query);
  if (match == null || !match.Success) {
    throw new UIException("ConnectionString not recognized as part of activation Uri.");
  }
  return HttpUtility.UrlDecode(match.Groups["ConnectionString"].Value);