大家好!现在我在做的一个程序用了一点SQL SERVER数据库,我想在别人的机子里运行这个程序可以自动生成数据库,怎样才能做到?
解决方案 »
- 求C# winform中点击按钮执行网页JS提交表单代码实现~~!
- 取值问题,也许非常简单
- 怎样动态的赋值给windows-mediaplayer?
- c# 如何接收推送数据
- "对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。"
- [求助]为什么继承了ToolBar的控件override的它的OnPaint方法却不能执行?
- 怎么做新的语言?
- 怎样才能使应用程序在开机时自动运行!
- bolb类型如何处理?
- 用 HttpListener 做web服务器,输出html,如何让html中外调的css和js起作用?
- 问个很菜的问题~~
- 窗口程序,运行是无异常,点击关闭按钮后控制台输出InteropServices.SEHException第一次偶然出现异常
或远程连接你的数据库
安装程序制作方法重点介绍如何在安装包中自动为客户创建数据库 步骤:
1、添加一个新项目-> 选择类库模板-> 命名为DBCustomAction 2、单击项目右键-> 添加新项-> 选择安装程序类(命名为DBCustomAction.cs) 3、添加一个新项sql.txt(注意要使用小写),输入建表、视图、存储过程等sql语句,例如:
CREATE TABLE [dbo].[MK_Employees] (
[Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Rsvp] [int] NULL ,
[Requests] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]; ALTER TABLE [dbo].[MK_Employees] WITH NOCHECK ADD
CONSTRAINT [PK_MK_Employees] PRIMARY KEY CLUSTERED
(
[Name]
) ON [PRIMARY];
(P.S:也可以直接用SqlServer导出) 4、在sql.txt的右键属性中-> 生成操作-> 嵌入的资源 5、将DBCustomAction.cs切换到代码视图,添加下列代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Collections.Specialized;
using System.Reflection;namespace DBCustomAction
{
/// <summary>
/// DBCustomAction 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class DBCustomAction : System.Configuration.Install.Installer
{
private System.Data.SqlClient.SqlConnection sqlConn;
string strDBName = string.Empty;
/// <summary>
/// 必需的设计器变量。
/// </summary>
//private System.ComponentModel.Container components = null;public DBCustomAction()
{
// 该调用是设计器所必需的。
InitializeComponent();// TODO: 在 InitComponent 调用后添加任何初始化
}#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConn = new System.Data.SqlClient.SqlConnection();
}
#endregionprivate string GetSql(string Name)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + ". "+Name);
StreamReader reader = new StreamReader(strm,System.Text.Encoding.Default);
string tmpstr;
string str = string.Empty;
reader.BaseStream.Seek(0, SeekOrigin.Begin);
while (reader.Peek() > -1)
{
tmpstr = reader.ReadLine().Trim();
if (tmpstr.ToUpper() == "GO ")
{
ExecuteSql(str);
str = string.Empty;
}
else if ( tmpstr.Length> 3 && tmpstr.ToUpper().Substring(0,4) == "USE ")
{
strDBName = tmpstr.Substring(4).Trim();
}
else
{
str += tmpstr + "\r\n ";
}
}
return str;
}
catch (Exception ex)
{
Console.Write( "In GetSql: "+ex.Message);
throw ex;
}
} private void ExecuteSql(string Sql)
{
if (Sql.Trim() != string.Empty)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);
Command.Connection.Open();
Command.Connection.ChangeDatabase(strDBName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
}
protected void AddDBTable()
{
try
{
strDBName = "master ";
ExecuteSql( "CREATE DATABASE " + this.Context.Parameters[ "dbname "]);
strDBName = this.Context.Parameters[ "dbname "];
ExecuteSql(GetSql( "sql.txt "));
}
catch(Exception ex)
{
Console.Write( "In exception handler : "+ex.Message);
}
} public override void Install(System.Collections.IDictionary stateSaver)
{
this.sqlConn.ConnectionString = "data source= "+this.Context.Parameters[ "dbserver "]+ ";initial catalog=master;password= "+this.Context.Parameters[ "password "]+ ";persist security " +
" info=True;user id= "+this.Context.Parameters[ "username "]+ ";packet size=4096 ";
base.Install(stateSaver);
AddDBTable();
} }
}
注意GetSql()中的
StreamReader reader = new StreamReader(strm,System.Text.Encoding.Default);
以便能正确的显示出中文
注意其他标记红色的地方6、再添加一个新项目,(选择添加到解决方案中)-> 项目类型为安装项目-> 命名为WebSetup 7、选择应用程序文件夹-> 添加-> 项目输出-> 主输出 、内容文件,以选取需要安装到目标机器的内容。
右键点“目标机器上的文件系统”可以添加字体、注册表信息等。8、在方案资源管理器中-> 右键安装项目(DBCustomAction Installer)-> 视图-> 用户界面 9、选中启动结点-> 添加对话框可以添加产品信息、用户使用许可协议等,并可以设置相关的文档(必须是rtf文档)11、选中启动结点-> 添加对话框-> 文本A 12、选动文本框A-> 右键-> 上移或者下移到合适的位置
13、选择文本框A属性-> 修改BannerText,(Specify Database Name) 14、修改BodyText(This dialog allows you to specify the name of the database to be created on the database server. ) 15、修改EditLabel1(Name of DB),修改Edit1Porperty(CUSTOMTEXTA1),其他的见上图 16、在方案资源管理器中-> 右键安装项目(WebSetup)-> 视图-> 自定义操作 17、选中安装结点-> 添加-> 双击应用程序文件夹-> 主输出来自DBCustomAction(活动)-> 右键属性-> CustomActiveData属性修改为
/dbserver=[CUSTOMTEXTA9] /username=[CUSTOMTEXTA8] /password=[CUSTOMTEXTA7] /dbname=[CUSTOMTEXTA1]18、编译生成(一定要关闭当前打开的所有文档),OK!