欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統 1904 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條評論
主站蜘蛛池模板: 91短视频app下载 | 白天操夜夜操 | 久久精品桃花综合 | 福利片在线| 亚洲精品乱码8久久久久久日本 | 欧美日韩一区二区视频在线观看 | 欧美精品综合 | 一级黄色免费毛片 | 亚洲欧美日韩一区二区在线观看 | 鲍蕾鲍莉| 免费一区 | 九九热视频精品在线 | 午夜在线电影 | 老司机午夜性大片 | 久久精精| 亚洲日韩中文字幕一区 | 中国一级特黄真人毛片免费看 | 九九免费在线视频 | 九九热色 | 日本福利一区二区 | 久久一本久综合久久爱 | 欧美free性 | 成人午夜视频免费 | 亚洲人人爱| 国产小视频在线 | 久久免费国产视频 | 亚洲精品vr一区 | 久久久蜜桃| 中文字幕二区 | 久久97久久 | 羞羞电影在线观看 | 午夜国产精品免费观看 | 欧美三极 | 我和我的祖国电影在线观看免费版高清 | 欧美一区高清 | 成人网av| 欧美在线播放一区 | 一区二区在线看 | 波多野结衣三级在线观看 | 午夜大片| 久久99深爱久久99精品 |