国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 02:29:38
字體:
來源:轉載
供稿:網友

   在.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 的摘要說明。
/// </summary>
[runinstaller(true)]
public class dbcustomaction : system.configuration.install.installer
{
/// <summary>
///@author:overred
/// </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
}
}



收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岱山县| 涟水县| 巴里| 永年县| 登封市| 泾川县| 台东市| 广西| 克什克腾旗| 新平| 隆化县| 宜都市| 长汀县| 黄石市| 宁化县| 化德县| 新民市| 饶河县| 久治县| 烟台市| 麻栗坡县| 齐河县| 呼图壁县| 高雄县| 金坛市| 阳东县| 宁蒗| 体育| 绥宁县| 安阳县| 茶陵县| 股票| 玉门市| 江孜县| 沂水县| 兴隆县| 永吉县| 明水县| 唐海县| 漠河县| 临汾市|