C#安装项目文件,自动读取参数的安装类(自动安装SQL SERVER数据库)
2006-05-17 17:28
507 查看
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Data.SqlClient;
using System.Reflection;
namespace DBCustomAction
{
/// <summary>
/// DBCustomAction 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string strPass = "";
public DBCustomAction()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
public override void Install(System.Collections.IDictionary stateSaver)
{
//入口
strPass = this.Context.Parameters["strPass"];
AddDBTable("RequestSys");//RequestSys为数据库名称
}
private string GetSql(string strName)
{
try
{
//' Get the current assembly.
Assembly Asm = Assembly.GetExecutingAssembly();
// Resources are named using a fully qualified name
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + strName);
//Read the contents of the embedded file.
StreamReader reader= new StreamReader(strm);//,System.Text.Encoding.Unicode);
return reader.ReadToEnd();
}
catch
{
return null;
}
}
private void ExecuteSql(string DatabaseName , string Sql)
{
SqlConnection sqlConnection1 = new SqlConnection("user id=sa;password="+strPass+";database=master;server=(local)") ;
SqlCommand Command = new SqlCommand(Sql, sqlConnection1);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
// Finally, blocks are a great way to ensure that the connection
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName )
{
try
{
//Create the database.
ExecuteSql("master", "CREATE DATABASE " + strDBName);
// Create the tables.
ExecuteSql(strDBName, GetSql("sql.txt"));
}
catch
{
}
}
}
}
附:
自定义操作设置
CustomActionData
属性两个值以上设置方法如下:
/strPass=[STRPASS] /edit2=[EDITC2]
参考
http://msdn2.microsoft.com/zh-tw/library/2w2fhwzz.aspx
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Data.SqlClient;
using System.Reflection;
namespace DBCustomAction
{
/// <summary>
/// DBCustomAction 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string strPass = "";
public DBCustomAction()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
public override void Install(System.Collections.IDictionary stateSaver)
{
//入口
strPass = this.Context.Parameters["strPass"];
AddDBTable("RequestSys");//RequestSys为数据库名称
}
private string GetSql(string strName)
{
try
{
//' Get the current assembly.
Assembly Asm = Assembly.GetExecutingAssembly();
// Resources are named using a fully qualified name
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + strName);
//Read the contents of the embedded file.
StreamReader reader= new StreamReader(strm);//,System.Text.Encoding.Unicode);
return reader.ReadToEnd();
}
catch
{
return null;
}
}
private void ExecuteSql(string DatabaseName , string Sql)
{
SqlConnection sqlConnection1 = new SqlConnection("user id=sa;password="+strPass+";database=master;server=(local)") ;
SqlCommand Command = new SqlCommand(Sql, sqlConnection1);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
// Finally, blocks are a great way to ensure that the connection
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName )
{
try
{
//Create the database.
ExecuteSql("master", "CREATE DATABASE " + strDBName);
// Create the tables.
ExecuteSql(strDBName, GetSql("sql.txt"));
}
catch
{
}
}
}
}
附:
自定义操作设置
CustomActionData
属性两个值以上设置方法如下:
/strPass=[STRPASS] /edit2=[EDITC2]
参考
http://msdn2.microsoft.com/zh-tw/library/2w2fhwzz.aspx
相关文章推荐
- C# 获取文件路径,读取项目中某程序集下文件
- C# 保存图像文件到SQL SERVER数据库,再读取该文件并显示
- C#项目中读取并修改App.config文件
- c# 打包程序中自动附加安装SQL Server数据库
- C#项目实例中读取并修改App.config文件
- C#项目打包,并自动安装SQL数据库
- [转载] C#项目打包,并自动安装SQL数据库
- C#项目实例中读取并修改App.config文件
- C#项目打包,并自动安装SQL数据库
- c# winform项目treeview控件绑定本地电脑磁盘信息,读取文件夹信息,显示文件夹内文件内容
- C#项目打包,并自动安装SQL数据库
- C#生成安装文件后自动附加数据库的思路跟算法
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- C#程序读取Excel文件后读入DataSet,提示错误:找不到可安装的ISAM
- C#生成安装文件后自动附加数据库的思路跟算法
- C#项目打包,并自动安装SQL数据库
- 项目加载时,配置文件读取参数并把参数放在缓存中。
- C#项目打包,并自动安装SQL数据库
- 【转】C#项目打包,并自动安装SQL数据库
- C#项目打包,并自动安装SQL数据库