.添加部署数据库的类文件DataBase.cs,在下面的安装程序类文件WebInstaller.cs里将使用此类
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Xml;
using System.Configuration.Install;
using System.Windows.Forms;
namespace WebSetupLib
{
/** <summary>
/// DataBase 的摘要说明。
/// </summary>
public class DataBase
{
string serverName=null;
string databaseName =null;
string userName=null;
string Password=null;
bool trustedconnection=false;
string targetPath=null;
XmlDocument config=null;
public const string CONST_DATBASE_PLACEHOLDER ="<<DATABASE_NAME>>";
//构造方法
public DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream("WebSetupLib.Resources.DataBase.xml");
config=new XmlDocument();
config.Load(stream);
}
public DataBase(string servername,string databasename,string username,
string password,string targetpath):this()
{
this.serverName=servername;
this.databaseName=databasename;
this.userName=username;
this.Password=password;
this.targetPath=targetpath;
}
/** <summary>
/// 信任连接
/// </summary>
/// <param name="servername"></param>
/// <param name="databasename"></param>
/// <param name="targetpath"></param>
public DataBase(string servername,string databasename,string targetpath):this()
{
this.serverName =servername;
this.databaseName=databasename;
this.trustedconnection=true;
this.targetPath =targetpath;
}
/** <summary>
/// 安装数据库
/// </summary>
/// <returns></returns>
public bool CreateDataBase()
{
string fileName=null;
try
{
ProcessStartInfo processInfo=new ProcessStartInfo("osql.exe");
processInfo.WindowStyle=ProcessWindowStyle.Hidden;
//Get the name of the file from the assembly's embedded resource.
//MessageBox.Show("到这一步了吗?");
if(config !=null)
{
fileName = config.SelectSingleNode("configroot/Files/DataBase/Add
/File").Attributes["name"].Value;
//MessageBox.Show(fileName);
}
else
{
throw new InstallException("创建数据库的脚本文件不存在!");
}
//Get arguments
processInfo.Arguments=GetCommonProcessArguments(fileName,"master");
//EventLog.WriteEntry("安装数据库",processInfo.Arguments);
//不知道为什么,不能写日志,提示日志满?
//MessageBox.Show(GetFullPath(fileName).ToString());
PopulateDatabaseNamePlaceHolder(GetFullPath(fileName));
Process osql = Process.Start(processInfo);
//Wait till it is done
osql.WaitForExit();
//EventLog.WriteEntry("安装数据库","数据库创建完成..");
osql.Dispose();
return true;
}
catch(Exception ex)
{
//Customize if required.
//EventLog.WriteEntry("安装数据库",ex.Message,EventLogEntryType.Error); throw new InstallException(ex.Message);
}
}
/** <summary>
/// 删除数据库
/// </summary>
/// <returns></returns>
public bool DropDataBase()
{
string fileName=null;
try
{
ProcessStartInfo processInfo =new ProcessStartInfo("osql.exe");
processInfo.WindowStyle=ProcessWindowStyle.Hidden;
//Get the name of the file from the assembly's embedded resource.
if(config !=null)
{
fileName = config.SelectSingleNode("configroot/Files/DataBase/Remove
/File").Attributes["name"].Value;
}
else
{
throw new InstallException("删除数据库的脚本文件不存在!");
}
//Get arguments
processInfo.Arguments=GetCommonProcessArguments(fileName,"master");
//EventLog.WriteEntry("安装数据库",processInfo.Arguments);
PopulateDatabaseNamePlaceHolder(GetFullPath(fileName));
Process osql = Process.Start(processInfo);
osql.WaitForExit();
//EventLog.WriteEntry("安装数据库","删除数据库");
osql.Dispose();
return true;
}
catch(Exception ex)
{
//Customize if required.
//EventLog.WriteEntry("安装数据库",ex.Message,EventLogEntryType.Error); throw new InstallException(ex.Message);
}
}
/** <summary>
/// 插入数据
/// </summary>
/// <returns></returns>
public bool InsertDate()
{
try
{
ExecuteScripts("configroot/Files/Insert/File");
//EventLog.WriteEntry("安装数据库","记录增加完成");
return true;
}
catch(Exception ex)
{
//Customize if required.
//EventLog.WriteEntry("安装数据库",ex.Message,EventLogEntryType.Error); throw new InstallException(ex.Message);
}
}
/** <summary>
/// 执行数据库脚本
/// </summary>
/// <param name="Xpath"></param>
void ExecuteScripts(string Xpath)
{
XmlNodeList objectlist=null;
ProcessStartInfo processInfo =new ProcessStartInfo("osql.exe");
processInfo.WindowStyle=ProcessWindowStyle.Hidden;
//Get the name of the file from the assembly's embedded resource.
if(config !=null)
{
objectlist = config.SelectNodes(Xpath);
}
else
{
throw new InstallException("保存数据库脚本文件名称的XML文件不存在!");
}
foreach(XmlNode objectFile in objectlist)
{
//Get arguments
processInfo.Arguments=GetCommonProcessArguments(objectFile.Attributes
["name"].Value,databaseName);
//EventLog.WriteEntry("安装数据库",processInfo.Arguments);
Process osql = Process.Start(processInfo);
//Wait till it is done
osql.WaitForExit();
//EventLog.WriteEntry("安装数据库", objectFile.InnerText +" file
//executed..");
}
}
/** <summary>
/// 获得数据库脚本的完整路径
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private string GetFullPath(string fileName)
{
//The destination folder for this will be the Install/scripts folder under the
//installation root
//MessageBox.Show(targetPath + @"SqlScript/" + fileName);
string FullPath = targetPath + @"SqlScript/" + fileName;
return FullPath;
}
/** <summary>
/// 读数据库脚本文件的内容
/// </summary>
/// <param name="filepath"></param>
void PopulateDatabaseNamePlaceHolder(string filepath)
{
//Read the contents
System.IO.StreamReader reader = new System.IO.StreamReader(filepath);
string content=reader.ReadToEnd();
reader.Close();
//Replace the placeholder with database name
content =content.Replace(CONST_DATBASE_PLACEHOLDER,databaseName);
//Writeback the contents
System.IO.StreamWriter writer = new System.IO.StreamWriter(filepath,false);
//MessageBox.Show(content);
writer.Write(content);
writer.Flush();
writer.Close();
}
string GetCommonProcessArguments(string fileName, string overridendatabasename)
{
//是否是信任连接
if(!trustedconnection)
{
/**//* Arguments configured.
* osql [-S server] [-d use database name] [-U login id] [-P password]
* [-i inputfile] [-o outputfile]
* */
return " -S " + serverName + " -d " + overridendatabasename +" -U " + username
+ " -P " +Password + " -i " + Char.ToString('"') + GetFullPath(fileName)
+ Char.ToString('"') + " -o " + Char.ToString('"') + targetPath + "Log.txt"
+ Char.ToString('"');
}
else
{
/**//* Arguments configured.
* osql [-S server] [-d use database name] [-E trusted connection]
* [-i inputfile] [-o outputfile]
* */
return " -S " + serverName + " -d " + overridendatabasename + " -E " + " –i
" + Char.ToString('"') + GetFullPath(fileName) + Char.ToString('"') +
" -o " + Char.ToString('"') + targetPath + "Log.txt" + Char.ToString('"');
}
}
}
}