c#打包mysql配置文件_C#打包SQL数据库部署安装

参考《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

{

///

/// DBCustomAction 的摘要说明。

///

[RunInstaller(true)]

public class DBCustomAction : System.Configuration.Install.Installer

{

///

/// 必需的设计器变量。

///

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();

}

}

///

/// 清理所有正在使用的资源。

///

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 组件设计器生成的代码

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

components = new System.ComponentModel.Container();

}

#endregion

}

}

我不需要修改Web.config的部分.

注意.如果不用SA用户登录数据库的,请先在服务器上建立特定的SQL用户

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 读取保存App.config配置文件的完整源码参考(转) http://smartsoft.5d6d.com/thread-6550-1-1.html C# 读取保存App.config配置文件的完整源码参考 最近出差在北京做一个小项目,项目里需要读取配置文件的小功能,觉得挺有参考意义的就把代码发上来给大家参考一下。我们选择了直接用微软的读取配置文件的方法。 这个是程序的运行设计效果,就是把这些参数可以进行灵活设置,灵活保存设置状态。 程序编译后自动会产生相应的配置文件,是跟项目的名称一样的配置文件。 读取配置文件及保存配置的具体代码参考如下,希望能给你节省一些时间,直接复制粘贴这个代码就可以用了: //------------------------------------------------------------ // All Rights Reserved , Copyright (C) 2010 , CDPF , Ltd. //------------------------------------------------------------ using System; using System.Configuration; using System.Windows.Forms; using Utilities; namespace DirectSeeding { /// /// FrmConfig /// 读取配置文件 /// /// 修改纪录 /// /// 2011.01.14 版本: 1.0 JiRiGaLa 完善程序的注释等、从新整理代码。 /// /// 版本:1.0 /// /// /// JiRiGaLa /// 2011.01.14 /// /// public partial class FrmConfig : Form { public FrmConfig() { InitializeComponent(); } /// /// 读取配置文件 /// private void GetConfig() { this.txtWriteFileName.Text = ConfigurationManager.AppSettings["WriteFileName"]; this.txtWritePath.Text = ConfigurationManager.AppSettings["WritePath"].Replace("|", Environment.NewLine); this.txtPostMessageURL.Text = ConfigurationManager.AppSettings["PostMessageURL"]; this.txtLeasedLineURL.Text = ConfigurationManager.AppSettings["LeasedLineURL"]; } private void FrmDirectSeeding_Load(object sender, EventArgs e) { this.GetConfig(); } /// /// 保存配置文件 /// private void SaveConfig() { // 写入参数设置 Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings["WriteFileName"].Value = this.txtWriteFileName.Text; configuration.AppSettings.Settings["Wri

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值