installdb.vb类,要添加引用 system.configuration.install.dll :
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Reflection;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace install
{
/// <summary>
/// Installer1 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class Installer1 : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Installer1()
{
// 该调用是设计器所必需的。
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
private string GetSql(string Name)
{
// //调用osql执行脚本
//
// System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
//
// sqlProcess.StartInfo.FileName = "osql.exe";
//
// sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", this.Context.Parameters["user"], this.Context.Parameters["pwd"],"master", this.Context.Parameters["targetdir"]);
//
// sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
// sqlProcess.Start();
//
// sqlProcess.WaitForExit() ;//等待执行
//
// sqlProcess.Close();
try
{
// Assembly Asm = Assembly.GetExecutingAssembly();
// System.IO.FileInfo FileInfo = new System.IO.FileInfo(Asm.Location);
// string path=FileInfo.DirectoryName+@"\"+Name;
string path=this.Context.Parameters["targetdir"]+Name;
FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
StreamReader reader = new StreamReader(fs,System.Text.Encoding.Default);
//System.Text.Encoding.ASCII;
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
private void ExecuteSql(string DataBaseName,string Sql)
{
SqlConnection sqlConnection1=new SqlConnection();
sqlConnection1.ConnectionString =string.Format("server={0}; user id={1}; password={2}; Database=master",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
try
{
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
Command.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master","CREATE DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("sql.txt"));
ExecuteSql("master","exec sp_addlogin 'myoamaster','myoamaster','"+strDBName+"',Null,Null");
ExecuteSql(strDBName,"EXEC sp_grantdbaccess 'myoamaster', 'myoamaster'");
ExecuteSql(strDBName,"exec sp_addrolemember 'db_owner','myoamaster'");
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable(this.Context.Parameters["dbname"]);
}
}
}
这里有个sql.txt是数据库的sql脚本,当然可以调用osql来执行sql脚本,其实是一样的。
打包的时候必须把sql.txt文件加进来,否则不会执行。
如果你想附加数据库的mdf文件和ldf文件,用下面这段程序:
private void CreateDataBase(string strSql,string DataName,string strMdf,string strLdf)
{
String str;
SqlConnection myConn = new SqlConnection (strSql);
//EXEC sp_detach_db @dbname = 'BX_FreightMileage_2'//需要先将数据库分离出来
str = "EXEC sp_attach_db @dbname = '"+ DataName +"', @filename1 = '"+ strMdf +"',@filename2='"+strLdf+"'";
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
}
当然打包的时候也要把这两个数据库文件也加进来。