Esempio n. 1
0
        public static bool UpdateConnectionString( )
        {
            Project pjt = Kit.GetProjectByName(Kit.SlnKeel.UIProjectName);

            if (pjt == null)
            {
                Common.ShowInfo("您尚未设置UI主界面项目, 因此无法自动配置连接字符串到你的app.config 或者web.config中!");
                return(true);
            }
            ProjectItem PI = null;

            for (int i = 1; i <= pjt.ProjectItems.Count; i++)
            {
                ProjectItem pix = pjt.ProjectItems.Item(i);
                if (pix.Name.ToLower().Contains("app.config") || pix.Name.ToLower().Contains("web.config"))
                {
                    PI = pix;
                    break;
                }
            }
            if (PI == null)
            {
                Common.ShowInfo(string.Format("您设置的主UI项目{0}中未找到app.config 或者web.config,因此无法自动设置链接字符串配置", pjt.Name));
            }
            else
            {
                bool needUpdate = true;

                ConfigXmlDocument cxd = new System.Configuration.ConfigXmlDocument();

                cxd.Load(PI.get_FileNames(1));
                XmlNode xnx  = cxd.SelectSingleNode("descendant::configuration/connectionStrings");
                string  csss = GetProjectConfigNamespec(GetProjectLangType(GetProjectUI()));

                string name = "";
                if (pjt.Kind == "{E24C65DC-7377-472b-9ABA-BC803B73C61A}")
                {
                    name = "Keel" + csss + ".ConnectionString";
                }
                else
                {
                    name = ((string)pjt.Properties.Item("RootNamespace").Value) + csss + ".ConnectionString";
                }
                if (xnx != null)
                {
                    for (int i = 0; i < xnx.ChildNodes.Count; i++)
                    {
                        if (xnx.ChildNodes[i].Attributes["name"].Value.Contains(name))
                        {
                            if (xnx.ChildNodes[i].Attributes["providerName"].Value != Kit.SlnKeel.ProviderName)
                            {
                                xnx.ChildNodes[i].Attributes["providerName"].Value = Kit.SlnKeel.ProviderName;
                            }
                            if (xnx.ChildNodes[i].Attributes["connectionString"].Value != Kit.SlnKeel.ConnectString)
                            {
                                xnx.ChildNodes[i].Attributes["connectionString"].Value = Kit.SlnKeel.ConnectString;
                            }
                            cxd.Save(PI.get_FileNames(1));
                            needUpdate = false;
                            break;
                        }
                    }
                }
                if (needUpdate)
                {
                    XmlNode      xn             = cxd.CreateNode("element", "add", "");
                    XmlAttribute xzproviderName = cxd.CreateAttribute("providerName");
                    xzproviderName.Value = Kit.SlnKeel.ProviderName;
                    XmlAttribute xaconnectionString = cxd.CreateAttribute("connectionString");
                    xaconnectionString.Value = Kit.SlnKeel.ConnectString;
                    XmlAttribute xaname = cxd.CreateAttribute("name");
                    xaname.Value = name;
                    xn.Attributes.Append(xaname);
                    xn.Attributes.Append(xaconnectionString);
                    xn.Attributes.Append(xzproviderName);
                    if (xnx == null)
                    {
                        XmlNode xnx1 = cxd.SelectSingleNode("descendant::configuration");
                        xnx = cxd.CreateElement("connectionStrings");
                        xnx1.AppendChild(xnx);
                    }
                    xnx.AppendChild(xn);
                    // cxd.SelectSingleNode("descendant::configuration/connectionStrings").InnerXml = xnx.InnerXml;
                    cxd.Save(PI.get_FileNames(1));
                    //;// = cxd.SelectSingleNode("descendant::configuration/connectionStrings/add[@name='" + appnamespace + ".Properties.Settings.ConnectionString']");
                    //if (xnl.Attributes["providerName"] != null && xnl.Attributes["connectionString"] != null)
                    //{

                    //}
                }
                Common.ShowInfo(string.Format("您设置的主UI项目{0}中到app.config 或者web.config,已经设置了链接字符串{1}", pjt.Name, name));
            }
            return(true);
        }