日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時間:2024/4/18 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#打包mysql配置文件_C#打包SQL数据库部署安装(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

參考《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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。