安装程序自动安装数据库


添加新项中添加安装程序类 

None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Configuration.Install;
None.gif
using System.IO;
None.gif
using System.Data.SqlClient;
None.gif
using System.Reflection;
None.gif
None.gif
None.gif
namespace DBCustomAction
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// DBCustomAction 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [RunInstaller(true)]
InBlock.gif    
public class DBCustomAction : System.Configuration.Install.Installer
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
/// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif        
private string strPass = "";
InBlock.gif
InBlock.gif        
public DBCustomAction()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
// 该调用是设计器所必需的。
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
// TODO: 在 InitComponent 调用后添加任何初始化
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gif        
Component Designer generated code
InBlock.gif
InBlock.gif        
public override void Install(System.Collections.IDictionary stateSaver)
ExpandedSubBlockStart.gif        
{
InBlock.gif        
InBlock.gif            
//入口
InBlock.gif
            strPass = this.Context.Parameters["strPass"];
InBlock.gif            AddDBTable(
"RequestSys");//RequestSys为数据库名称
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string  GetSql(string strName)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                
//' Get the current assembly.
InBlock.gif
                Assembly Asm = Assembly.GetExecutingAssembly();
InBlock.gif                
// Resources are named using a fully qualified name
InBlock.gif
                
InBlock.gif                Stream strm  
= Asm.GetManifestResourceStream(Asm.GetName().Name + "." + strName);
InBlock.gif                
InBlock.gif                
//Read the contents of the embedded file.
InBlock.gif
                StreamReader reader= new StreamReader(strm);//,System.Text.Encoding.Unicode);
InBlock.gif
            
InBlock.gif                
InBlock.gif                
return reader.ReadToEnd();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif                                                                                                                                      
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void ExecuteSql(string DatabaseName , string Sql)
ExpandedSubBlockStart.gif        
{
InBlock.gif
InBlock.gif            SqlConnection sqlConnection1 
= new SqlConnection("user id=sa;password="+strPass+";database=master;server=(local)") ;
InBlock.gif            SqlCommand Command  
= new SqlCommand(Sql, sqlConnection1);
InBlock.gif            Command.Connection.Open();
InBlock.gif            Command.Connection.ChangeDatabase(DatabaseName);
InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                Command.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
finally
ExpandedSubBlockStart.gif            
{
InBlock.gif                
// Finally, blocks are a great way to ensure that the connection 
InBlock.gif
                Command.Connection.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected void  AddDBTable(string strDBName )
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                
//Create the database.
InBlock.gif
                ExecuteSql("master""CREATE DATABASE " + strDBName);
InBlock.gif                
// Create the tables.
InBlock.gif
                ExecuteSql(strDBName, GetSql("sql.txt"));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gif            
{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/25/262046.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值