安装程序自动安装数据库

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

None.gif using  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.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DBCustomAction 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [RunInstaller(true)]
InBlock.gif    
public class DBCustomAction : System.Configuration.Install.Installer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 该调用是设计器所必需的。
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
// TODO: 在 InitComponent 调用后添加任何初始化
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Component Designer generated code#region Component Designer generated code
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
public override void Install(System.Collections.IDictionary stateSaver)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif                                                                                                                                      
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void ExecuteSql(string DatabaseName , string Sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Command.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将SQL Server数据库安装包和WPF程序安装包一起打包成一个安装程序,并实现自动安装数据库和WPF程序,可以按照以下步骤操作: 1. 将SQL Server数据库安装包和WPF程序安装包放置在同一文件夹中。 2. 使用Visual Studio创建一个新的安装程序项目。对于Visual Studio 2019及更高版本,可以选择Visual Studio Installer项目模板。 3. 在安装程序项目中添加需要安装的文件,包括SQL Server数据库安装包、WPF程序安装包和其他必需的文件。 4. 在安装程序项目中添加自定义操作。对于Visual Studio Installer项目,可以使用Custom Actions功能来实现自定义操作。 5. 在自定义操作中添加以下代码来安装SQL Server: ``` string arguments = "/QS /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /FEATURES=SQL /INSTANCENAME=MSSQLSERVER /SQLSVCACCOUNT=\"NT AUTHORITY\\Network Service\" /SQLSYSADMINACCOUNTS=\"BUILTIN\\Administrators\" /TCPENABLED=1 /NPENABLED=1 /SECURITYMODE=SQL /SAPWD=\"myPassword123\""; ProcessStartInfo startInfo = new ProcessStartInfo("SQLEXPR_x64_ENU.exe", arguments); startInfo.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(startInfo).WaitForExit(); ``` 该代码将执行SQL Server安装程序,并使用指定的参数进行安装。您可以根据自己的需求修改参数。请注意,此代码仅适用于64位版本的SQL Server。 6. 在自定义操作中添加以下代码来安装WPF程序: ``` string arguments = "/SILENT /NORESTART /DIR=\"[TARGETDIR]\""; ProcessStartInfo startInfo = new ProcessStartInfo("MyWPFAppInstaller.msi", arguments); startInfo.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(startInfo).WaitForExit(); ``` 该代码将执行WPF程序的安装程序,并使用指定的参数进行安装。您需要将"MyWPFAppInstaller.msi"替换为实际的WPF程序安装包名称。请注意,此代码仅适用于MSI安装程序。 7. 编译并测试您的安装程序。 以上步骤将帮助您将SQL Server数据库安装包和WPF程序安装包一起打包成一个安装程序,并实现自动安装数据库和WPF程序的功能。请注意,在安装过程中,SQL Server安装程序可能需要管理员权限才能运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值