惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Hacker News: Front Page
S
Security Affairs
Google Online Security Blog
Google Online Security Blog
Attack and Defense Labs
Attack and Defense Labs
H
Heimdal Security Blog
S
Securelist
S
Secure Thoughts
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
N
News | PayPal Newsroom
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
O
OpenAI News
V
Vulnerabilities – Threatpost
S
Schneier on Security
Cyberwarzone
Cyberwarzone
雷峰网
雷峰网
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
美团技术团队
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
T
Tor Project blog
P
Privacy International News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
S
Security @ Cisco Blogs
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
Schneier on Security
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
aimingoo的专栏
aimingoo的专栏
L
LINUX DO - 热门话题
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
www.infosecurity-magazine.com
www.infosecurity-magazine.com
U
Unit 42

博客园 - 武广敬

There is no mode by that name loaded / mode not given 产生原因(个案) 使用VS调试JS 使用U盘安装Windows Server2008 德广火车票助手登录12306代码详解-登录 德广火车票助手源码 请各位前辈给些建议 用户中心 - 博客园 vs2008 SmartDevice 程序 访问Internet时出错 提示:未能建立与网络的连接。解决方案 关于微软有自增列父子表更新程序的问题 免费的网上问卷调查程序 在线HTML标签验证工具.很好用的. 微软扶持软件公司计划 新鲜出炉的Asp.Net MVC电子书 我理解的云计算 设定了窗口的AcceptButton及CancelButton点击时无动作的原因 使用电脑+MODEM 订火车票 (可以一直自动重拨) 黄牛党/铁路内部人员不得进入!!! UI设计注意点 可以让DataGridView中多个ComboBox级联的工具类-初版 通过反射得到对象 DataGridViewComboBoxColumn 可以让用户输入并自动匹配选项的问题 [问了很多人,找了很多站都没得到答案,所以只好放首页,请管理员体谅.谢谢!]
使用Visual Studio生成安装程序并设定连接字符串及其他自定义操作. - 武广敬 - 博客园
武广敬 · 2009-03-17 · via 博客园 - 武广敬

应该很多人都有遇到这个问题.现在给个完整的解决方案.

1.添加一个安装项目(当然你的其他项目应该都已经OK了.现在我们已经做好了一个WinUI的项目.记得要建立App.config文件,而且要有连接字符串的配置节存在.不然之后会出错.因为我没做错误处理.)

目前的App.config文件内容,connectionString值为空,需要用户在安装时输入.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ConnectionString" connectionString="" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

2.在安装项目中添加项目输出,如UI层,业务逻辑层,数据操作层,实体层,公共层等项目输出.

3.选中左面的用户桌面,在右边右键创建新的快捷方式.

4.生成安装项目,就可以生成安装程序了.不过还没完呢.

5.安装过程中需要用户输入连接数据库的信息或建立数据库等操作.需要对安装过程进行定制.在用户界面视图中进行.

6.添加让用户输入信息的对话框

7. 修改文本框的属性.最多可以有4个文本框.定义窗口的标题(BannerText),窗口的描述信息(BodyText),各文本框标签的值 (Edit_Label),对应的属性名(Edit_Property),默认值(Edit_Value),是否可见(Edit_Visible)等属 性.

8.要新建一个类库项目来实现定制安装的功能.

9.建立功能实现类

10.代码如下(请注意看代码注释):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Configuration;
using System.Xml;
using System.IO;

namespace SetSetup
{
    /// <summary>
    /// 继承安装类
    /// www.szitr.com
    /// </summary>
    [RunInstaller(true)]
    public partial class SetSqlConStr : Installer
    {
        public SetSqlConStr()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 重写基类的安装方法
        /// </summary>
        /// <param name="stateSaver"></param>
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            //得到用户输入的参数
            //参数来自于后面 第 14 步 自定义操作的参数
            //自定义操作参数的属性CustomActionData:/SqlServerIP=[SQLSERVER_NAME] /DataBase=[DATABASE_NAME] /UserName=[USERNAME] /Password=[PASSWORD] /TargetDir="[TARGETDIR]\"
            //中括号中的就是之前各文本框的Edit_Property值.最后一个TARGETDIR是安装路径,注意后面还有个反斜杆
            string sqlServerIP = this.Context.Parameters["SqlServerIP"];
            string database = this.Context.Parameters["DataBase"];
            string userName = this.Context.Parameters["UserName"];
            string password = this.Context.Parameters["Password"];
            string targetdir = this.Context.Parameters["TargetDir"];
            //这里写你要执行的代码
            //组合连接字符串
            string conString = String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};Persist Security Info=True", sqlServerIP, database, userName, password);
            //更新连接字串设定值,WinUI.exe.config 要改成你设定档的名称
            UpdateConConfig("ConnectionString", conString, targetdir + "WinUI.exe.config");
        }

        /// <summary>
        /// 修改设定档连接字符串的值
        /// </summary>
        /// <param name="conName">连接字符串名称</param>
        /// <param name="conString">连接字符串</param>
        /// <param name="configfilePath">设定档路径及名称</param>
        public static void UpdateConConfig(string conName, string conString,string configfilePath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //读取设定档
            xmlDoc.Load(configfilePath);
            //取得连接字符串的节点
            XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/connectionStrings/add[@name='" + conName + "']");
            //修改连接字符串
            xmlNode.Attributes["connectionString"].InnerText = conString;
            //保存
            xmlDoc.Save(configfilePath);
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);
        }
        public override void Rollback(System.Collections.IDictionary savedState)
        {
            base.Rollback(savedState);
        }
    }
}

11.回到安装项目.打开自定义操作视图

12.在安装中添加自定义操作

13.添加刚刚第9步所建立的项目的主输出

14.修改此自定义操作的CustomActionData属性

CustomActionData:/SqlServerIP=[SQLSERVER_NAME] /DataBase=[DATABASE_NAME] /UserName=[USERNAME] /Password=[PASSWORD] /TargetDir="[TARGETDIR]\"

15.重新生成一次.再执行安装程序看看.已经OK啦!

最后看看我们的设定档内容

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=DBServer;Initial Catalog=szitrDB;User ID=szitr.com;Password=szitr.com;Persist Security Info=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

成功!

希望对各位有帮助.

代码下载:http://szitr.com/bbs/thread-131-1-1.html