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

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

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

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

l???????? 安裝數據庫

微軟msdn上介紹過在部署應用程序的時候建立數據庫。如:

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

這種方法是創建一個安裝程序類,在安裝程序類中調用ado.net執行sql 語句(SQL語句放在一個文本文件中)來創建數據庫。

?

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

最好的辦法是調用osql執行腳本。(或者創建一個數據庫項目的cmd文件,而cmd文件建立數據庫的時候也是調用的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>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;

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

// TODO: 在 InitializeComponent 調用后添加任何初始化
}

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


#region 組件設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </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"]);

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

//調用osql執行腳本
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();//等待執行
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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品视频国产 | 国产精品视频一区二区三区 | a一级毛片免费播放 | 国产高清在线视频一区二区三区 | 欧美综合图区亚洲综合图区 | 亚洲视频在线免费观看 | 天天操你 | 久久视频免费 | 日日夜人人澡人人澡人人看免 | 国产精品亚洲综合第一区 | 99久久精品国产一区二区成人 | 国产大战女模特在线视频 | 99视频精品国在线视频艾草 | 久久久国产高清 | 免费一级毛片不卡在线播放 | 青草五月天| 成人夜色香网站在线观看 | 欧美性猛交xxxx免费看久久久 | 天天综合网天天综合色不卡 | 国产一二精品 | 亚洲男女在线 | 337p欧洲亚洲大胆艺术 | 久久综合精品视频 | 波多野结衣一区二区 三区 波多野结衣一区二区三区 波多野结衣一区二区三区高清在线 | 99热久久国产精品这里有全部 | 国产网址 | 久久综合九色欧美综合狠狠 | 最近在线更新中文字幕1 | 亚洲第一区二区快射影院 | 日日拍夜夜嗷嗷叫国产 | 一区在线观看 | 青草青在线免费视频 | 日日干夜夜爱 | 免费一级毛片在线播放欧美 | 日本一区二区三区中文字幕 | 亚洲精品短视频 | 毛片成人永久免费视频 | 九九久久国产精品大片 | 午夜欧美视频 | 国产精品久久久久aaaa | 国产精品视_精品国产免费 国产精品视频2021 |