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
{
///
/// 必需的設(shè)計器變量。
///
private System.ComponentModel.Container components = null;
public DBCustomAction()
{
// 該調(diào)用是設(shè)計器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 調(diào)用后添加任何初始化
}
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);
// ------------------------建立數(shù)據(jù)庫-------------------------------------------------
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"]);
//'根據(jù)輸入的數(shù)據(jù)庫名稱建立數(shù)據(jù)庫
ExecuteSql(connstr, "master", "CREATE DATABASE " +Context.Parameters["dbname"]);
//'調(diào)用osql執(zhí)行腳本
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(); // '等待執(zhí)行
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中的節(jié)點
//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連接字符串設(shè)置");
}
xmldocument.Save(fileinfo.FullName);
}
catch(Exception ex)
{
throw ex;
}
*/
}
#region 組件設(shè)計器生成的代碼
///
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
///
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
我不需要修改Web.config的部分.
注意.如果不用SA用戶登錄數(shù)據(jù)庫的,請先在服務(wù)器上建立特定的SQL用戶
.
總結(jié)
以上是生活随笔為你收集整理的c#打包mysql配置文件_C#打包SQL数据库部署安装(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: access 更新整列数据_在acces
- 下一篇: linux cmake编译源码,linu