当前位置:编程学习 > C#/ASP.NET >>

请教关于数据库

大家好!现在我在做的一个程序用了一点SQL SERVER数据库,我想在别人的机子里运行这个程序可以自动生成数据库,怎样才能做到? --------------------编程问答-------------------- 生成SQL脚本 在别人的机器里跑。。
或远程连接你的数据库 --------------------编程问答--------------------

安装程序制作方法

重点介绍如何在安装包中自动为客户创建数据库  

步骤:  
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();
}
#endregion

private     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! 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,