亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 1800 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)機(jī)器的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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 老司机福利免费 | 激情伊人网 | 久久久久久久男人的天堂 | 日日摸日日添夜夜爽97 | 成人欧美一区二区三区视频 | 中国妞xxx的视频 | 国产在线精品一区二区 | 人体做爰aaaa免费 | 四虎成人影院网址 | 色婷婷国产 | 中文字幕无线码中文字幕免费 | 97se狠狠狠狠狼亚洲综合网 | 国产美女福利视频 | 神马影院我不卡888 神马影院我不卡手机 | 香蕉在线精品一区二区 | 四虎新地址 | 69一级毛片| 久久成人毛片 | aaa级片 | 欧美专区一区二区三区 | 成人在线视频国产 | 青青青免费手机版视频在线观看 | 成人a在线| 毛片5| 亚洲第二页 | 久久久久久久久久爱 | 一级做a爱片久久毛片 | 精品国产欧美一区二区 | 国产视频www | 亚洲欧美在线精品一区二区 | 国产亚洲精品一区二区久久 | 久久私人影院 | 91亚洲国产在人线播放午夜 | 99er这里只有精品 | 在线视频亚洲 | 色天天综合 | 久久精品国产400部免费看 | 国产一级特黄高清免费大片 | 亚洲免费不卡 | 久久精品国产一区二区三区不卡 | 欧美丰满大乳大屁股毛片 |