这个思路是在昨天写了一个数据库安装类以后想及的,昨天稍微研究了一下自定义安装的东西,感觉里面东西很多,以前我一直在找wise和install shield的for 。net的程序,但现在觉得,对于一般应用而言,用。net自己带的工具是绰绰有余了。
我目前的想法是让安装程序除了建立虚拟目录以外还得建立数据库,省得用户的手工操作,那样就会避免很多麻烦。这个工作我昨天晚上在家的时候做了,今天我想我是不是应该能建立这样的一个工具,能够让别的开发人员省掉这一步,包括导数据结构和建立安装时候的数据库设置窗体,我想用插件的形式做这个工作。
显然bigeagle知道我们大部分人的毛病,太浮躁了,不能把一件事情很专心的做好,于我而言,前辈的话是要听的,毛病还是要犯的,所以我决定做完第一步以后剩下的事情就先不理了,:)
其实只是因为前些日子论坛上的一些事情,我希望能够通过自己的努力让更多人去关注自己应该关注的东西,去珍惜这个网上家园。
废话说了这么多,:(
————————————————————
文件sqldmodemo.cs,实现操作的类
using system;
using sqldmo;
using system.windows.forms;
namespace generatesqlscript
{
/// <summary>
/// sqldmodemo 的摘要说明。
/// </summary>
public class sqldmodemo
{
private const sqldmo_script_type sqldmoscript_drops = sqldmo_script_type.sqldmoscript_drops;
private const sqldmo_script_type sqldmoscript_includeheaders = sqldmo_script_type.sqldmoscript_includeheaders;
private const sqldmo_script_type sqldmoscript_default = sqldmo_script_type.sqldmoscript_default;
private const sqldmo_script_type sqldmoscript_appendtofile =sqldmo_script_type.sqldmoscript_appendtofile;
private const sqldmo_script_type sqldmoscript_bindings = sqldmo_script_type.sqldmoscript_bindings;
private sqldmo.sqldmo_script_type intoptions;
private sqldmo.sqldmo_script2_type int2options;
public sqldmodemo()
{
//
// todo: 在此处添加构造函数逻辑
//
this.intoptions = sqldmoscript_drops | sqldmoscript_includeheaders | sqldmoscript_default | sqldmoscript_appendtofile | sqldmoscript_bindings;
this.int2options = sqldmo_script2_type.sqldmoscript2_default;
}
/// <summary>
/// 导出script的函数
/// </summary>
/// <param name="strservername"></param>
/// <param name="strusername"></param>
/// <param name="strpassword"></param>
/// <param name="strdatabase"></param>
/// <param name="strfilepath"></param>
public void generatesqlscript(string strservername,string strusername,string strpassword,string strdatabase,string strowner,string strfilepath)
{
try
{
sqldmo.sqlserver sql = new sqldmo.sqlserver();
sqldmo.database db = new sqldmo.database();
sqldmo.trigger trigger = new sqldmo.trigger();
//连接数据库
sql.connect(strservername,strusername,strpassword);
db = (sqldmo.database)sql.databases.item(strdatabase,strowner);
//导出自定义类型
foreach (sqldmo.userdefineddatatype objgen in db.userdefineddatatypes)
{
objgen.script(intoptions,strfilepath,int2options);
}
//导出表和触发器,过滤掉系统表
foreach (sqldmo.table objtable in db.tables)
{
if (objtable.systemobject == false)
{
objtable.script(intoptions,strfilepath,null,int2options);
foreach(sqldmo.trigger objtrigger in objtable.triggers)
{
if (objtrigger.systemobject == false)
{
objtrigger.script(intoptions,strfilepath,int2options);
}
}
}
}
//导出规则
foreach (sqldmo.rule objrule in db.rules)
{
objrule.script(intoptions,strfilepath,int2options);
}
//导出存储过程
foreach (sqldmo.storedprocedure objprocedure in db.storedprocedures)
{
if (objprocedure.systemobject == false)
{
objprocedure.script(intoptions,strfilepath,int2options);
}
}
foreach (sqldmo.view objview in db.views)
{
if (objview.systemobject == false)
{
objview.script(intoptions,strfilepath,int2options);
}
}
messagebox.show ("成功啦,恭喜,恭喜");
}
catch(exception e)
{
messagebox.show(e.message);
throw (e);
}
}
}
}
————————————————————
测试程序form1.cs
————————————————————
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace generatesqlscript
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button1 = new system.windows.forms.button();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(336, 128);
this.button1.name = "button1";
this.button1.tabindex = 0;
this.button1.text = "button1";
this.button1.click += new system.eventhandler(this.button1_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(568, 341);
this.controls.addrange(new system.windows.forms.control[] {
this.button1});
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
sqldmodemo demo = new sqldmodemo();
demo.generatesqlscript("(local)","sa","www.topcoolsite.com","bbs","dbo","c:\\aa.sql");
}
}
}
————————————————————
感谢bigeagle、怡红公子、开心就好、jh.mei在本人书写本文时的帮助。
