--------------------------------第一步------------------------------------------------
新建一个安装项目,右键点“setup1”---“视图”---“用户界面”,右键“启动”---“添加对话框”,添加一个“对话框A”,并且移动到最上面。
打开“对话框A”的属性
edit1property设为server
edit2property设为uid
edit3property设为pwd
edit4property设为database
---------------------------------第二步------------------------------------------------
在同一解决方案中新建一个类库项目
using System;
using System.Collections.Generic;
using System.Text;
//以下为添加的名字空间
using System.Configuration.Install;//需要添加引用
using System.Data.SqlClient;
using System.ComponentModel;
using System.IO;
namespace ClassInstallDB
{
[RunInstaller(true)] //添加
public class ClassInstall:Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);//这个方法不能删除
string sql = "create database test "+
" go "+
" use test "+
" go "+
" if exists (select * from sysobjects where id = OBJECT_ID('[TB_Image]') and OBJECTPROPERTY(id, 'IsUserTable') = 1) "+
" DROP TABLE [TB_Image] "+
" CREATE TABLE [TB_Image] ( "+
" [Img_ID] [varchar] (50) NOT NULL, "+
" [ImageFile] [image] NULL, "+
" [UpdateTime] [datetime] NOT NULL DEFAULT (getdate()), "+
" [LastViewTime] [datetime] NULL DEFAULT (getdate()))";//sql脚本
string server = this.Context.Parameters["server"];//用户填写的服务器名或地址
string uid = this.Context.Parameters["uid"];//用户填写的登录sql的用户名
string pwd = this.Context.Parameters["pwd"];//用户填写的登录sql的密码
string conStr = string.Format("server={0};uid={1};pwd={2};database=master",server,uid,pwd);
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
}
---------------------------------第三步------------------------------------------------
- 编译上面的类库项目。
- 右键“setup1”---“视图”---“文件系统”。
- 在“引用程序文件夹”上右键“添加”---“项目输出”,将刚编译的类库输出添加进来。
- 右键“setup1”---“视图”---“自定义操作”。
- 在“安装”上右键“添加自定义操作”,双击“应用程序文件夹”,双击“主输出来自。。。。”
- 右键“setup1”---“生成”
- 运行生成的安装包即可在安装是处理数据库操作