安装程序自动安装数据库
来源:互联网 发布:小学音乐教学软件 编辑:程序博客网 时间:2024/06/10 08:59
添加新项中添加安装程序类
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
{
}
}
}
}
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
{
}
}
}
}
- 安装程序自动安装数据库
- .NET程序打包(自动安装数据库)总结
- C#程序打包,自动安装SQL数据库
- 让程序自动安装
- CppCMS程序自动安装
- 怎样在打包程序中自动安装SQL Server数据库?
- 在打包程序中自动安装SQL Server数据库
- 在打包程序中自动安装SQL Server数据库
- 在打包程序中自动安装SQL Server数据库
- 在打包程序中自动安装SQL Server数据库
- 在打包程序中自动安装SQL Server数据库
- c# 打包程序中自动附加安装SQL Server数据库
- WinForm程序打包实现自动安装数据库简介
- 数据库安装程序
- Windows: 远程自动安装程序
- RAR制作自动安装程序
- 自动安装补丁的程序
- VC实现程序自动安装
- url传递中文的解决方案
- 排序和顺序统计学——堆排序
- 学如驾车登山,不进就退;学如一逆水行舟,不进则退
- ActiveMQ4.1 +Spring2.0的POJO JMS方案(上)
- ActiveMQ4.1 +Spring2.0的POJO JMS方案(下)
- 安装程序自动安装数据库
- 程序安装时检查是否已经安装.NETFramework
- 设计模式--工厂方法
- OpenGL显示ASE文件格式的MESH
- JAVA笔试之初始化顺序
- iptables 实现NAT功能(利用iptables实现局域网共享上网)
- Exception 处理之最佳实践
- 网页 的 40大 常用技巧
- [数学题] 证明:(km+1)(km+2)...(km+m)能整除m!( k是一个整数)