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类中,添加如下代码

None.gif // GetSql实现从指定文本文件中读取SQL语句
None.gif
         private   string  GetSql( string  strName) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                Assembly Asm 
= Assembly.GetExecutingAssembly();
InBlock.gif                Stream strm
=Asm.GetManifestResourceStream(Asm.GetName().Name+"."+strName);
InBlock.gif                StreamReader reader 
= new StreamReader(strm);
InBlock.gif                
return reader.ReadToEnd();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch (Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                Console.Write(
"In GetSql:"+ex.Message); 
InBlock.gif                
throw ex; 
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }
 
None.gif
None.gif        
// ExecuteSql实现执行指定SQL语句
None.gif
         private   void  ExecuteSql ( string  conn,  string  Sql) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            SqlConnection myConnection
=new SqlConnection(conn); 
InBlock.gif            SqlCommand myCommand
=new SqlCommand(Sql,myConnection); 
InBlock.gif            myConnection.Open(); 
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                myCommand.ExecuteNonQuery(); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                myConnection.Close(); 
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }
 
None.gif
None.gif        
// 重载ExecuteSql
None.gif
         private   void  ExecuteSql( string  conn, string  DataBaseName, string  Sql) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            SqlConnection myConnection
=new SqlConnection(conn); 
InBlock.gif            SqlCommand myCommand
=new SqlCommand(Sql,myConnection); 
InBlock.gif            myConnection.Open(); 
InBlock.gif            myCommand.Connection.ChangeDatabase(DataBaseName); 
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                myCommand.ExecuteNonQuery(); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                myConnection.Close(); 
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }
 
None.gif
None.gif        
// 此处完成安装过程所做的自定义操作
None.gif
         public   override   void  Install(System.Collections.IDictionary stateSaver) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
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"]);//获取数据连接字符串 
InBlock.gif                
//根据下面的数据库名创建数据库 
InBlock.gif
                ExecuteSql(connStr, "CREATE DATABASE "+Context.Parameters["dbname"]); 
InBlock.gif                
//执行SQL创建表(视图等) 
InBlock.gif
                ExecuteSql(connStr,Context.Parameters["dbname"],GetSql("TestDB.sql"));     
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch (Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw ex; 
ExpandedSubBlockEnd.gif            }
          
ExpandedBlockEnd.gif        }
 
在类库中添加TestDB.sql,并且把TestDB.sql的"生成操作"属性设置为"嵌入的资源"   TestDB.sql中的代码如下:
None.gif if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[TABLE1] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
None.gif
drop   table   [ dbo ] . [ TABLE1 ]
None.gif
None.gif
None.gif
CREATE   TABLE   [ dbo ] . [ TABLE1 ]  (
None.gif    
[ ttt ]   [ char ]  ( 10 ) COLLATE Chinese_PRC_CI_AS  NULL  
None.gif
ON   [ PRIMARY ]
None.gif
None.gif
insert   into  table1(ttt)  values ( ' 23232 ' )

转载于:https://www.cnblogs.com/syringa-flz/archive/2006/11/02/547674.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值