C# 与 SQL2005数据库 打包

目的 : 在用户服务器端生成空数据库 . 并用自写的服务器程序读写数据库

使用本方式首先要在用户机器上安装 SQL2005, 然后方可顺利安装 .

 

一 ). 创建部署项目
1. 打开 VS.NET2005 。
2 .在 “ 文件 ” 菜单上指向 “ 新建项目 ” 。
3. 在 “ 新建项目 ” 对话框中,选择 “ 项目类型 ” 窗格 中的 ” 其他项目类型 ” 中的 “ 安装和部署 ” ,然后选择 “ 模板 ” 窗格中的 “ 安装项目 ” 。在 “ 名称 ” 框中键入 Setup1 。
4. 单击 “ 确定 ” 关闭对话框。
5. 项目被添加到解决方案资源管理器中,并且文件系统编辑器打开。如下图所示:
 
6. 在 “ 属性 ” 窗口中,选择 ProductName 属性,并键入 数据库打包安装。

 

二 ). 创建安装程序类
1. 在 “ 解决方案资源管理器 ” 中的 ” 解决方 Setup1” 上右键选择 ” 添加 ” 后选择 “ 新建项目 ” 。
2. 在 “ 添加新项目 ” 对话框中,选择 “ 项目类型 ” 窗格中的 “Visual C#” 下的 ”Windows” ,然后选择 “ 模板 ” 窗格中的 “ 类库 ” 。在 “ 名称 ” 框中键入 InstallDB 。
3. 单击 “ 确定 ” 关闭对话框。
4. 从 “InstallDB” 类库下右键选择 ” 添加 ” 中的 ” 新建项 ” 。
5. 在 “ 添加新项 ” 对话框中选择 “ 安装程序类 ” 。在 “ 名称 ” 框中键入 InstallDB 。
6. 单击 “ 添加 ” 关闭对话框。
7. “InstallDB” 安装程序类详细代码附后。
如下图所示 :
 
三 ). 创建自定义安装对话框
1. 在解决方案资源管理器中选择 “setup1” 项目。右键然后在 “ 视图 ” 菜单上选择 “ 用户界面 ” 。
2. 在用户界面编辑器中,选择 “ 安装 ” 下的 “ 启动 ” 节点选择 “ 添加对话框 ” 。
3 在 “ 添加对话框 ” 对话框中,选择 “ 文本框 (A)” 对话框,然后单击 “ 确定 ” 关闭对话框。
4. 在 “ 文本框 (A)” 右键菜单上,选择 “ 上移 ” 。重复此步骤,直到 “ 文本框 (A)” 对话框位于 “ 安装文件夹 ” 节点之上。
5 在 “ 文本框 (A)” 的 “ 属性 ” 窗口中,选择 BannerText 属性并键入:安装数据库。
6. 选择 BodyText 属性并键入:安装程序将在目标机器上安装数据库。
7. 选择 Edit1Label 属性并键入:数据库名称 :
8 选择 Edit1Property 属性并键入 DBNAME
9. 选择 Edit1Value 属性并键入: budgetSysDB
10. 选择 Edit2Label 属性并键入:服务器名 :
11. 选择 Edit2Property 属性并键入 SERVER
12. 选择 Edit2Value 属性并键入: (local)
13. 选择 Edit3Label 属性并键入:用户名 :
13. 选择 Edit3Value 属性并键入: sa
15. 选择 Edit3Property 属性并键入 USER
16. 选择 Edit4Label 属性并键入:密码 :
18. 选择 Edit4Property 属性并键入 PWD
19. 选择 Edit1Visible 属性,设置为 false( 数据库名称用不到 ); 选择 Edit2Visible 、 Edit3Visible 和 Edit4Visible 属性,并将它们设置为 true
 

 

四 ). 创建自定义操作
1. 在解决方案资源管理器中选择 “setup1” 项目 , 右键在 “ 视图 ” 菜单上选择 “ 自定义操作 ” 。
2. 在自定义操作编辑器中选择 “ 安装 ” 节点。右键选择 “ 添加自定义操作 ” 。
3. 在 “ 选择项目中的项 ” 对话框中,双击 “ 应用程序文件夹 ” 。
4. 单击 ” 添加输出 ” 按钮选择 ” 项目 ” 中的 ” InstallDB”, 在下面框中选择 ” 主输出 ”( 此项一般为默认 ) ,然后单击 “ 确定 ” 关闭对话框。
5. 在 ” 主输出来自 InstallDB( 活动 ) 的属性窗口中,选择 CustomActionData 属性并键入: /dbname=[DBNAME] /server=[SERVER] /user=[USER] /pwd=[PWD]  /targetdir="[TARGETDIR]\" 。 ( 注意空格 !!)
附 :/targetdir="[TARGETDIR]\" 是安装后的目标路径,为了在 installDB 类中获得安装后的路径,我们设置此参数。
  如下图所示:
 

 

五 ) 生成数据库

1. 在自己的 SQL 数据库 DB 上 ( 例如数据库名称为 :DB), 右击 -> 任务 -> 生成脚本 . 将该脚本存储为 Mydb2005.sql

2. 编辑 ”Mydb2005.sql”, 并在文档前段附加以下内容 :

-- 生成数据库

use master

if not exists( select name from sysdatabases where name = ' 你要生成的数据库名称 ' )

begin

create database 你要生成的数据库名称

end

go

 

-- 创建登录名 / 密码

IF not   EXISTS ( SELECT * FROM sys.server_principals WHERE name = N' 你的登录名 ' )

begin

create login [succeed] with PASSWORD = 你的密码 , CHECK_EXPIRATION= OFF , CHECK_POLICY= ON

end

go

 

use MineMonitoringSystem

-- 添加用户

IF NOT EXISTS ( SELECT * FROM sys.database_principals WHERE name = N' 你的登录名 ' )

CREATE USER [ 你的用户名称 ] FOR LOGIN [ 你的登录名 ] WITH DEFAULT_SCHEMA= [dbo]

GO

-- 分配角色

exec   sp_addrolemember   'db_owner' , ' 你的用户名称 '

go

后面是 SQL 自动生成的脚本 , 主要是创建表格 , 存储过程之类的 .

注意 : 添加用户可能会在 SQL 自动生成的脚本中存在 , 注意不要重复 .

 

六 ).”InstallDB” 安装程序类代码

view plaincopy to clipboardprint?
namespace  InstallDB  
{  
    [RunInstaller (true )]  
    public partial class InstallerDB : Installer  
    { 
#if DEBUG  
        System .IO .StreamWriter _file = new System .IO .StreamWriter (@"D:\server.txt" , false );           // 测试用 
#endif  
        public InstallerDB ()  
        {  
            InitializeComponent ();  
        }  
   
        public override void Install (System .Collections .IDictionary stateSaver )  
        {  
            base .Install (stateSaver );  
   
            if (!this .CreateDataBase ())  
            {// 失败,反安装  
                this .Uninstall (stateSaver );  
                return ;  
            }  
        }  
   
        private bool CreateDataBase ()  
        {  
            try 
            {  
                 string server = this .Context .Parameters ["  server  " ];  
                string uid = this .Context .Parameters [" user " ];  
                string pwd = this .Context .Parameters [" pwd " ];  
                string path = this .Context .Parameters [" targetdir " ];  
                string strSql = " server= " + server + " ;uid= " + uid + " ; pwd= " + pwd + " ;database=master " ;  
   
                // 调用OSQL 执行脚本  
                System .Diagnostics .Process sqlProcess = new System .Diagnostics .Process ();  
                 sqlProcess .StartInfo .FileName = "sqlcmd.exe" ;  
                sqlProcess .StartInfo .Arguments = string .Format ("-U {0} -P {1} -S {2} -i \"{3}Mydb2005.sql\"" ,  
                    this .Context .Parameters ["user" ],  
                    this .Context .Parameters ["pwd" ],  
                    this .Context .Parameters ["server" ],  
                    this .Context .Parameters ["targetdir" ]);  
   
   
                sqlProcess .StartInfo .WindowStyle = System .Diagnostics .ProcessWindowStyle .Hidden ;  
                sqlProcess .Start ();  
                sqlProcess .WaitForExit ();  
                sqlProcess .Close (); 
  
#if DEBUG  
                _file .WriteLine (this .Context .Parameters ["server" ]);  
                _file .WriteLine (this .Context .Parameters ["user" ]);  
                _file .WriteLine (this .Context .Parameters [" pwd " ]);  
                _file .WriteLine (this .Context .Parameters [" targetdir " ]);  
                _file .WriteLine (sqlProcess .StartInfo .Arguments );  
                _file .Close (); 
#endif  
   
   
                return true ;  
            }  
            catch 
            {  
                return false ;  
            }  
   
        }  
public override void Uninstall (System .Collections .IDictionary savedState )  
        {  
            base .Uninstall (savedState );  
        }  
   
   
        private void DeleteFile (string paths )  
        {  
            try 
            {  
                System .IO .FileInfo delFile = new System .IO .FileInfo (paths );  
   
                if (delFile .Exists )  
                {  
                    delFile .Delete ();  
                }  
            }  
            catch { }  
        }  
   
    }  

namespace  InstallDB
{
    [RunInstaller (true )]
    public partial class InstallerDB : Installer
    {
#if DEBUG
        System .IO .StreamWriter _file = new System .IO .StreamWriter (@"D:\server.txt" , false );           // 测试用
#endif
        public InstallerDB ()
        {
            InitializeComponent ();
        }
 
        public override void Install (System .Collections .IDictionary stateSaver )
        {
            base .Install (stateSaver );
 
            if (!this .CreateDataBase ())
            {// 失败,反安装
                this .Uninstall (stateSaver );
                return ;
            }
        }
 
        private bool CreateDataBase ()
        {
            try
            {
                 string server = this .Context .Parameters ["  server  " ];
                string uid = this .Context .Parameters [" user " ];
                string pwd = this .Context .Parameters [" pwd " ];
                string path = this .Context .Parameters [" targetdir " ];
                string strSql = " server= " + server + " ;uid= " + uid + " ; pwd= " + pwd + " ;database=master " ;
 
                // 调用OSQL 执行脚本
                System .Diagnostics .Process sqlProcess = new System .Diagnostics .Process ();
                 sqlProcess .StartInfo .FileName = "sqlcmd.exe" ;
                sqlProcess .StartInfo .Arguments = string .Format ("-U {0} -P {1} -S {2} -i \"{3}Mydb2005.sql\"" ,
                    this .Context .Parameters ["user" ],
                    this .Context .Parameters ["pwd" ],
                    this .Context .Parameters ["server" ],
                    this .Context .Parameters ["targetdir" ]);
 
 
                sqlProcess .StartInfo .WindowStyle = System .Diagnostics .ProcessWindowStyle .Hidden ;
                sqlProcess .Start ();
                sqlProcess .WaitForExit ();
                sqlProcess .Close ();
 
#if DEBUG
                _file .WriteLine (this .Context .Parameters ["server" ]);
                _file .WriteLine (this .Context .Parameters ["user" ]);
                _file .WriteLine (this .Context .Parameters [" pwd " ]);
                _file .WriteLine (this .Context .Parameters [" targetdir " ]);
                _file .WriteLine (sqlProcess .StartInfo .Arguments );
                _file .Close ();
#endif
 
 
                return true ;
            }
            catch
            {
                return false ;
            }
 
        }
public override void Uninstall (System .Collections .IDictionary savedState )
        {
            base .Uninstall (savedState );
        }
 
 
        private void DeleteFile (string paths )
        {
            try
            {
                System .IO .FileInfo delFile = new System .IO .FileInfo (paths );
 
                if (delFile .Exists )
                {
                    delFile .Delete ();
                }
            }
            catch { }
        }
 
    }
}

不足之处,欢迎指正!

原文地址:https://www.cnblogs.com/hfzsjz/p/1640170.html