vc#打包部署全攻略之(三、SQLServer2000打包)
方法一:
新建一个项目Installer.创建一个应用程序。
新建一个安装部署程序dbSetup
新建一个类库dbClassLibrary 添加一个"安装程序类"Installer1.cs
在dbSetup中的【文件系统】的应用程序文件夹中添加dbSetup和dbClassLibrary的主输出。
在dbSetup中的【用户界面】启动中添加文本框(A)设置属性如下:
Edit1Label 数据库:
Edit1Property CUSTOMTEXTA1
Edit1Value TestDB
Edit2Label 服务器:
Edit2Property CUSTOMTEXTA2
Edit2Value (local)
Edit3Label 用户名:
Edit3Property CUSTOMTEXTA3
Edit3Value sa
Edit4Label 密码:
Edit4Property CUSTOMTEXTA4
Edit4Value sa
在dbSetup中的【自定义操作】的安装中添加自定义操作"主输出来自dbClassLibrary(活动)" 并且设置其CustomActionData属性为:/dbname=[CUSTOMTEXTA1] /server=[CUSTOMTEXTA2] /user=[CUSTOMTEXTA3] /pwd=[CUSTOMTEXTA4]
在Installer1.cs类中,添加如下代码
//GetSql实现从指定文本文件中读取SQL语句
private string GetSql(string strName)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm=Asm.GetManifestResourceStream(Asm.GetName().Name+"."+strName);
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
//ExecuteSql实现执行指定SQL语句
private void ExecuteSql (string conn, string Sql)
{
SqlConnection myConnection=new SqlConnection(conn);
SqlCommand myCommand=new SqlCommand(Sql,myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
}
}
//重载ExecuteSql
private void ExecuteSql(string conn,string DataBaseName,string Sql)
{
SqlConnection myConnection=new SqlConnection(conn);
SqlCommand myCommand=new SqlCommand(Sql,myConnection);
myConnection.Open();
myCommand.Connection.ChangeDatabase(DataBaseName);
try
{
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
}
}
//此处完成安装过程所做的自定义操作
public override void Install(System.Collections.IDictionary stateSaver)
{
try
{
string connStr=String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096",Context.Parameters["server"],Context.Parameters["user"],Context.Parameters["pwd"]);//获取数据连接字符串
//根据下面的数据库名创建数据库
ExecuteSql(connStr, "CREATE DATABASE "+Context.Parameters["dbname"]);
//执行SQL创建表(视图等)
ExecuteSql(connStr,Context.Parameters["dbname"],GetSql("TestDB.sql"));
}
catch (Exception ex)
{
throw ex;
}
}
在类库中添加TestDB.sql,并且把TestDB.sql的"生成操作"属性设置为"嵌入的资源" TestDB.sql中的代码如下:private string GetSql(string strName)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm=Asm.GetManifestResourceStream(Asm.GetName().Name+"."+strName);
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
//ExecuteSql实现执行指定SQL语句
private void ExecuteSql (string conn, string Sql)
{
SqlConnection myConnection=new SqlConnection(conn);
SqlCommand myCommand=new SqlCommand(Sql,myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
}
}
//重载ExecuteSql
private void ExecuteSql(string conn,string DataBaseName,string Sql)
{
SqlConnection myConnection=new SqlConnection(conn);
SqlCommand myCommand=new SqlCommand(Sql,myConnection);
myConnection.Open();
myCommand.Connection.ChangeDatabase(DataBaseName);
try
{
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
}
}
//此处完成安装过程所做的自定义操作
public override void Install(System.Collections.IDictionary stateSaver)
{
try
{
string connStr=String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096",Context.Parameters["server"],Context.Parameters["user"],Context.Parameters["pwd"]);//获取数据连接字符串
//根据下面的数据库名创建数据库
ExecuteSql(connStr, "CREATE DATABASE "+Context.Parameters["dbname"]);
//执行SQL创建表(视图等)
ExecuteSql(connStr,Context.Parameters["dbname"],GetSql("TestDB.sql"));
}
catch (Exception ex)
{
throw ex;
}
}
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TABLE1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TABLE1]
CREATE TABLE [dbo].[TABLE1] (
[ttt] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
insert into table1(ttt) values('23232')
drop table [dbo].[TABLE1]
CREATE TABLE [dbo].[TABLE1] (
[ttt] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
insert into table1(ttt) values('23232')