日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

部署安裝時寫入SQL SERVER和Web.config

系統(tǒng) 2142 0
本文轉(zhuǎn)自: http://aspxboy.com/private/5280/default.aspx
在.NET平臺下,部署 Web 解決方案是比較方便的。我們可以利用Visual Studio.NET 2003添加一個WEB安裝項目,在部署的“文件系統(tǒng)編輯器”中添加項目的主輸出和內(nèi)容文件,非常簡易地完成安裝程序的制作。

?????? 但是,這樣制作的安裝程序,只是將Web頁和ASP.NET程序編譯的DLL文件安裝到目標(biāo)機器的IIS目錄,對于一般的應(yīng)用程序是可以的(比如用Access數(shù)據(jù)庫,可以一起打包到安裝程序中);如果數(shù)據(jù)庫是SQL SERVER,需要在部署的時候一并安裝數(shù)據(jù)庫,安裝程序的制作就會復(fù)雜一些,需要我們自定義安裝程序類。在安裝程序類中執(zhí)行SQL腳本并將連接字符串寫入Web.config。

l???????? 安裝數(shù)據(jù)庫

微軟msdn上介紹過在部署應(yīng)用程序的時候建立數(shù)據(jù)庫。如:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkWalkthroughUsingCustomActionToCreateDatabaseDuringInstallation.asp

這種方法是創(chuàng)建一個安裝程序類,在安裝程序類中調(diào)用ado.net執(zhí)行sql 語句(SQL語句放在一個文本文件中)來創(chuàng)建數(shù)據(jù)庫。

?

但是,這種方法有一個問題,如果用sql Server2000生成了所有建表、視圖、存儲過程的一個腳本文件,用ADO.NET來執(zhí)行這個腳本文件,就會因為腳本中有許多“GO”語句而出現(xiàn)錯誤。當(dāng)然,我們可以把“GO”替換成換行符,利用ADO.NET一條條執(zhí)行SQL 語句。很顯然,這樣的效率比較低。

最好的辦法是調(diào)用osql執(zhí)行腳本。(或者創(chuàng)建一個數(shù)據(jù)庫項目的cmd文件,而cmd文件建立數(shù)據(jù)庫的時候也是調(diào)用的osql)。

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

namespace DBCustomAction
{
/// <summary>
/// DBCustomAction 的摘要說明。
///@ author:overred
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的設(shè)計器變量。
/// </summary>
private System.ComponentModel.Container components = null;

public DBCustomAction()
{
// 該調(diào)用是設(shè)計器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 調(diào)用后添加任何初始化
}

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


#region 組件設(shè)計器生成的代碼
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

#region custom setup

?

private void ExecuteSql(string connString,string DatabaseName,string sql)
{
SqlConnection conn=new SqlConnection(connString);
SqlCommand cmd=new SqlCommand(sql,conn);
conn.Open();
cmd.Connection.ChangeDatabase(DatabaseName);
try
{
cmd.ExecuteNonQuery();
}
catch(Exception e)
{
StreamWriter w=new StreamWriter(@"e:\\log.txt",true);
w.WriteLine("===in ExecuteSql======");
w.WriteLine(e.ToString());
w.Close();
}
finally
{
conn.Close();
}
}

public override void Install(IDictionary stateSaver)
{
createDB();
updateConfig();
}

private void createDB()
{
try
{
string connString=string.Format("server={0};user id={1};password={2}",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);

//根據(jù)輸入的數(shù)據(jù)庫名稱建立數(shù)據(jù)庫
ExecuteSql(connString,"master","create database "+this.Context.Parameters["dbname"]);

//調(diào)用osql執(zhí)行腳本
string cmd=string.Format(" -S{0} -U{1} -P{2} -d{3} -i{4}db.sql",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"],this.Context.Parameters["dbname"],this.Context.Parameters["targetdir"]);
System.Diagnostics.Process sqlProcess=new Process();
sqlProcess.StartInfo.FileName="osql.exe";
sqlProcess.StartInfo.Arguments=cmd;
sqlProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit();//等待執(zhí)行
sqlProcess.Close();

//刪除腳本文件
System.IO.FileInfo sqlFileInfo=new FileInfo(string.Format("{0}db.sql",this.Context.Parameters["targetdir"]));
if(sqlFileInfo.Exists)
sqlFileInfo.Delete();
}
catch(Exception e)
{
StreamWriter w=new StreamWriter(@"e:\log.txt",true);
w.WriteLine("===in Install======");
w.WriteLine(e.ToString());
w.Close();
}
}

private void updateConfig()
{
try
{
//將連接字符串寫入Web.config
System.IO.FileInfo fileInfo=new FileInfo(string.Format("{0}web.config",this.Context.Parameters["targetdir"]));

if(!fileinfo.exists)
throw new InstallException("can't find the web.config");

xmldocument doc=new XmlDocument();
doc.Load(fileInfo.FullName);
bool foundIt=false;

string connString=string.Format("server={0};database={1};user id={2};password={3}",this.Context.Parameters["server"],this.Context.Parameters["dbname"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);

string enCS=SecurityHelper.EncryptDBConnectionString(connString);

xmlnode no=doc.SelectSingleNode("http://appSettings/add[@key='connString']");
if(no!=null)
{
no.Attributes.GetNamedItem("value").Value=enCS;
foundIt=true;
}

if(!foundit)
throw new InstallException("can't find the connString setting ");
doc.Save(fileInfo.FullName);
}
catch(Exception e)
{
StreamWriter w=new StreamWriter(@"e:\log.txt",true);
w.WriteLine("===in updata connstring=tjtj=====");
w.WriteLine(e.ToString());
w.WriteLine(e.StackTrace);
w.Close();
}
}

#endregion
}
}

部署安裝時寫入SQL SERVER和Web.config


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宿松县| 聂荣县| 乳山市| 丘北县| 井研县| 友谊县| 玉龙| 承德市| 尚义县| 买车| 临沧市| 潮州市| 泸西县| 长宁区| 六盘水市| 宝山区| 涟水县| 都昌县| 镇雄县| 元阳县| 谢通门县| 赣榆县| 罗城| 汾西县| 龙游县| 合作市| 军事| 泰安市| 连城县| 兴和县| 锦屏县| 德庆县| 门源| 新和县| 突泉县| 长乐市| 汤原县| 建平县| 新邵县| 恭城| 东辽县|