仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1240|回复: 9
打印 上一主题 下一主题

[学习教程] ASP.NET网页编程之部署安装时写进SQL SERVER和Web.config...

[复制链接]
老尸 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:34:33 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
我觉得很重要,一般所说的不重要应该指的是:你学好一种以后再学另一种就很容易了。(因为这样大家可能有一个错觉就是语言不是很重要,只要随便学一种就可以了,其实不是这样的。server|sql|web在.NET平台下,部署Web办理计划是对照便利的。我们能够使用VisualStudio.NET2003增加一个WEB安装项目,在部署的“文件体系编纂器”中增加项目标主输入和内容文件,十分浅易地完成安装程序的制造。
可是,如许制造的安装程序,只是将Web页和ASP.NET程序编译的DLL文件安装到方针呆板的IIS目次,关于一样平常的使用程序是能够的(好比用Access数据库,能够一同打包到安装程序中);假如数据库是SQLSERVER,必要在部署的时分一并安装数据库,安装程序的制造就会庞大一些,必要我们自界说安装程序类。在安装程序类中实行SQL剧本并将毗连字符串写进Web.config。
  l安装数据库
  微软MSDN上先容过在部署使用程序的时分创建数据库。如:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkWalkthroughUsingCustomActionToCreateDatabaseDuringInstallation.asp
  这类办法是创立一个安装程序类,在安装程序类中挪用ADO.NET实行SQL语句(SQL语句放在一个文本文件中)来创立数据库。
  可是,这类办法有一个成绩,假如用SQLServer2000天生了一切建表、视图、存储历程的一个剧本文件,用ADO.NET来实行这个剧本文件,就会由于剧本中有很多“GO”语句而呈现毛病。固然,我们能够把“GO”交换成换行符,使用ADO.NET一条条实行SQL语句。很明显,如许的效力对照低。
  最好的举措是挪用osql实行剧本。(大概创立一个数据库项目标cmd文件,而cmd文件创建数据库的时分也是挪用的osql)。
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Configuration.Install;
usingSystem.Data.SqlClient;
usingSystem.IO;
usingSystem.Reflection;
usingSystem.Diagnostics;
usingSystem.Xml;
namespaceDBCustomAction
{
///<summary>
///DBCustomAction的择要申明。
///</summary>
[RunInstaller(true)]
publicclassDBCustomAction:System.Configuration.Install.Installer
{
///<summary>
///@author:overred
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
publicDBCustomAction()
{
//该挪用是计划器所必须的。
InitializeComponent();
//TODO:在InitializeComponent挪用后增加任何初始化
}
///<summary>
///清算一切正在利用的资本。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region组件计划器天生的代码
///<summary>
///计划器撑持所需的办法-不要利用代码编纂器修正
///此办法的内容。
///</summary>
privatevoidInitializeComponent()
{
components=newSystem.ComponentModel.Container();
}
#endregion
#regioncustomsetup
privatevoidExecuteSql(stringconnString,stringDatabaseName,stringsql)
{
SqlConnectionconn=newSqlConnection(connString);
SqlCommandcmd=newSqlCommand(sql,conn);
conn.Open();
cmd.Connection.ChangeDatabase(DatabaseName);
try
{
cmd.ExecuteNonQuery();
}
catch(Exceptione)
{
StreamWriterw=newStreamWriter(@"e:log.txt",true);
w.WriteLine("===inExecuteSql======");
w.WriteLine(e.ToString());
w.Close();
}
finally
{
conn.Close();
}
}
publicoverridevoidInstall(IDictionarystateSaver)
{
createDB();
updateConfig();
}
privatevoidcreateDB()
{
try
{
stringconnString=string.Format("server={0};userid={1};password={2}",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);
//依据输出的数据库称号创建数据库
ExecuteSql(connString,"master","createdatabase"+this.Context.Parameters["dbname"]);
//挪用osql实行剧本
stringcmd=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.ProcesssqlProcess=newProcess();
sqlProcess.StartInfo.FileName="osql.exe";
sqlProcess.StartInfo.Arguments=cmd;
sqlProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit();//守候实行
sqlProcess.Close();
//删除剧本文件
System.IO.FileInfosqlFileInfo=newFileInfo(string.Format("{0}db.sql",this.Context.Parameters["targetdir"]));
if(sqlFileInfo.Exists)
sqlFileInfo.Delete();
}
catch(Exceptione)
{
StreamWriterw=newStreamWriter(@"e:log.txt",true);
w.WriteLine("===inInstall======");
w.WriteLine(e.ToString());
w.Close();
}
}
privatevoidupdateConfig()
{
try
{
//将毗连字符串写进Web.config
System.IO.FileInfofileInfo=newFileInfo(string.Format("{0}web.config",this.Context.Parameters["targetdir"]));
if(!fileInfo.Exists)
thrownewInstallException("cantfindtheweb.config");
XmlDocumentdoc=newXmlDocument();
doc.Load(fileInfo.FullName);
boolfoundIt=false;
stringconnString=string.Format("server={0};database={1};userid={2};password={3}",this.Context.Parameters["server"],this.Context.Parameters["dbname"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);
stringenCS=SecurityHelper.EncryptDBConnectionString(connString);
XmlNodeno=doc.SelectSingleNode("//appSettings/add[@key=connString]");
if(no!=null)
{
no.Attributes.GetNamedItem("value").Value=enCS;
foundIt=true;
}
if(!foundIt)
thrownewInstallException("cantfindtheconnStringsetting");
doc.Save(fileInfo.FullName);
}
catch(Exceptione)
{
StreamWriterw=newStreamWriter(@"e:log.txt",true);
w.WriteLine("===inupdataconnstring=tjtj=====");
w.WriteLine(e.ToString());
w.WriteLine(e.StackTrace);
w.Close();
}
}
#endregion
}
}
我实在想不明白java的机制,为什么非要那么蛋疼,在同一个平台下重复编译。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-9 16:07

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表