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

推荐订阅源

博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
月光博客
月光博客
AI
AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
GbyAI
GbyAI
L
Lohrmann on Cybersecurity
O
OpenAI News
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Vercel News
Vercel News
The Hacker News
The Hacker News
Y
Y Combinator Blog
Latest news
Latest news
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cisco Blogs
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题

博客园 - 浪地

删除excel中重复的数据 - 浪地 - 博客园 SQL SERVER 与ACCESS、EXCEL的数据转换 水晶报表注册码 Request.ServerVariables的参数解析 - 浪地 - 博客园 Javascript中的函数替代ASP.NET的Server.UrlEncode C#实现的18位身份证格式验证算法(转) - 浪地 - 博客园 从SQL Server数据库提取图片并显示在DataGrid - 浪地 - 博客园 ASP.NET热点问题解答14个 把图片保存到sql server数据库里 JavaScript使用技巧精萃 - 浪地 - 博客园 T-SQL游标使用 SQL中通配符、转义符与"["号的使用 两台SQL Server数据同步解决方案 SQL Server中各个系统表的作用 SQL连接查询 SQL Server实用经验与技巧大汇集 SQLServer基本函数 从 HTTP 上下载文件示例 ASP.NET中17种正则表达式(转)
C#打包SQL数据库部署安装(原创啊)
浪地 · 2006-08-09 · via 博客园 - 浪地

参考《ASP.NET与SQL一起打包部署安装》,这篇文章是针对VB.NET与SQL 一起打包的,但是我使用的是C#,当然只要修改一下主要安装类库就行了!C#的类库代码如下:DBCustomAction.cs

using System;
using System.Collections;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;


namespace PMS
{
 /// <summary>
 /// DBCustomAction 的摘要说明。
 /// </summary>
 [RunInstaller(true)]
 public class DBCustomAction : System.Configuration.Install.Installer
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public DBCustomAction()
  {
   // 该调用是设计器所必需的。
   InitializeComponent();

   // TODO: 在 InitializeComponent 调用后添加任何初始化
  }

  private void ExecuteSql(string conn,string DatabaseName,string Sql)
  {
   SqlConnection mySqlConnection=new SqlConnection(conn);  
   SqlCommand Command=new SqlCommand(Sql, mySqlConnection);  
   mySqlConnection.Open();  
   mySqlConnection.ChangeDatabase(DatabaseName);  
   try
   {
    Command.ExecuteNonQuery();
   }  
   finally
   {
    //close Connection  
    mySqlConnection.Close();
   }
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  //

  public override void Install(System.Collections.IDictionary stateSaver)
  {
   base.Install(stateSaver);
  
   // ------------------------建立数据库-------------------------------------------------
  
   try
   {
    string connstr = String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", Context.Parameters["server"],Context.Parameters["user"], Context.Parameters["pwd"]);
    //'根据输入的数据库名称建立数据库  
    ExecuteSql(connstr, "master", "CREATE DATABASE " +Context.Parameters["dbname"]);  
    //'调用osql执行脚本  
    Process sqlprocess=new System.Diagnostics.Process();
    sqlprocess.StartInfo.FileName = "osql.exe ";  
    sqlprocess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", Context.Parameters["user"], Context.Parameters["pwd"],Context.Parameters["dbname"],Context.Parameters["targetdir"]);
    sqlprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    sqlprocess.Start();  
    sqlprocess.WaitForExit(); // '等待执行
    sqlprocess.Close();
  
    //'删除脚本文件
    FileInfo sqlfileinfo =new FileInfo(String.Format("{0}db.sql",Context.Parameters["targetdir"]));
  
    if (sqlfileinfo.Exists)
    {
     sqlfileinfo.Delete();
    }
   }
   catch(Exception ex)
   {
    throw ex;  
   }
  
   //' ---------------------将连接字符串写入Web.config-----------------------------------
  /*
   try
   {
    FileInfo fileinfo = new FileInfo(Context.Parameters["targetdir"] + "\\web.config");
    if (!fileinfo.Exists)
    {
     throw new InstallException("没有找到配置文件");
  
    }
  
    //'实例化xml文档
  
    XmlDocument xmldocument=new XmlDocument();
  
    xmldocument.Load(fileinfo.FullName);
  
  
  
    //'查找到appsettings中的节点
  
    //XmlNode node=new XmlNode();
  
    Boolean FoundIt  = false;
  
    foreach(XmlNode node in xmldocument.SelectSingleNode("appSettings").ChildNodes)
    {  
     if (node.Name == "add")
     {  
      if (node.Attributes.GetNamedItem("key").Value == "connString")
      {
       //'写入连接字符串
       node.Attributes.GetNamedItem("value").Value= String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1",Context.Parameters["server"],Context.Parameters["dbname"], Context.Parameters["user"], Context.Parameters["pwd"]);
       FoundIt= true;  
      }  
     }  
    }
  
    if (!FoundIt)
    {  
     throw new InstallException("web.Config 文件没有包含connString连接字符串设置");
    }   
    xmldocument.Save(fileinfo.FullName);
   }
   catch(Exception ex)
   {
    throw ex;
   } 
   */         
  }

  #region 组件设计器生成的代码
                 /// <summary>
                 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
                 /// 此方法的内容。
                 /// </summary>
                 private void InitializeComponent()
                 {
                  components = new System.ComponentModel.Container();
                 }
  #endregion
 }
}

我不需要修改Web.config的部分.
注意.如果不用SA用户登录数据库的,请先在服务器上建立特定的SQL用户
.