文档详情

打包安装数据库教程.doc

发布:2018-08-19约2.93千字共17页下载文档
文本预览下载声明
来源 : 北大青鸟 C#打包安装数据库教程 第一步 准备工作 新建一空白”解决方案” 分别在解决方案下面创建一个”安装项目”和”类库”项目 此项目是安装项目 此项目里后面会添加”安装类”来执行操作数据库的代码 把要打包执行的SQL语句保存到一个名为”sql.txt”的文件内,如: create table Students ( StudentID int identity(1,1) primary key, Name char(10) not null, Address nvarchar(50) , Grade int, Email nvarchar(50) ) insert into Students(Name,Address,Grade,Email) values(李明,扬州,1,liming@) 注意: 文件中不能有GO 第二步 设置”安装项目” 1. 添加SQL 文件 添加后的效果如上图 添加 DBCustomer项目的输出 添加以后效果如上图 设置安装 界面 点”属性窗口”,会看到如下图 修改属性 设置自定义操作 弹出如下窗体 双击 结果如下 在属性”CustomerActionData”下面写入以下信息 /server=[SERVER] /username=[USERNAME] /password=[PASSWORD] /databasename=[DATABASENAME] /targetdir=[TARGETDIR] 注: “/” 之前必须有空格 targetdir=[TARGETDIR] 是取得用户选择的安装路径 见图 到此,项目” DBSetup”就配置完成,下面设置项目” DBCustomer” 第三步 完成项目” DBCustomer” 弹出如下界面 点”添加” 出现如下图界面 实现代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.IO; using System.Data.SqlClient; namespace DBCustomer { [RunInstaller(true)] public partial class DBAction : Installer { public DBAction() { InitializeComponent(); } public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); #region 取得安装时用户输入的值 //这里取得的值就是用户输入的值 string server = this.Context.Parameters[server]; string name = this.Context.Parameters[username]; string pass = this.Context.Parameters[password]; string dbName = this.Context.Parameters[databasename]; string path = this.Context.Parameters[targetdir]; #endregion #region 读取要执行的SQL指令 FileStream fs = new FileStream(path+sql.txt, FileMode.Open); StreamReader sr = new StreamReader(fs); string sql = sr.ReadToEnd(); sr.Close(); fs.Close(); #endregion #region 执行创建数据库 string connString = string.Format(server={0};uid={1};pwd={
显示全部
相似文档