VS2005 通过SMO(SQL Management Objects) 管理 数据库的作业 警报 备份 等任务

1.安装SQLServer2005相关版本
2.在VS2005的项目中加入SMO相关参考
3.在VS2005的文件中 加入using
4.通过SMO相关类 进行对SQLServer的操作
-----
1.安装SQLServer2005相关版本
  以便能在 如下位置
  C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies
  得到相关 dll
  Microsoft.SqlServer.Smo.dll
  Microsoft.SqlServer.ConnectionInfo.dll
  ......
 
2.在VS2005的项目中加入SMO相关参考
  项目-右键-添加引用(参考)
 
3.在VS2005的文件中 加入using
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Smo.Broker;
其中还有其他一些空间 可以根据需要进行引用
using Microsoft.SqlServer.Management.Smo.CoreEnum;
using Microsoft.SqlServer.Management.Smo.Internal;
using Microsoft.SqlServer.Management.Smo.Mail;
using Microsoft.SqlServer.Management.Smo.RegisteredServers;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using Microsoft.SqlServer.Management.Smo.Wmi;

4.通过SMO相关类 进行对SQLServer的操作
示例代码如下:
protected void Button1_Click(object sender, EventArgs e)
    {
        //myServer
        Server myServer = new Server("192.168.0.36");
        Job myJob = new Job();
        Job myJob11 = new Job();
        //
        if (!myServer.JobServer.Jobs.Contains("myJob1"))
        {
            myJob = new Job(myServer.JobServer, "myJob1");
        }
        else
        {
            myJob11 = myServer.JobServer.Jobs["myJob1"];
            Random a = new Random();
            myJob = new Job(myServer.JobServer,"myJob"+a.Next().ToString());           
        }

        myJob.Description = "aaa";

        if (myJob.State.ToString() != "Existing")
        {
            myJob.Create();
        }
        Response.Write(myJob.State.ToString());
        Response.Write(myJob11.Name.ToString());       
    }

posted on 2006-12-14 17:52  freeliver54  阅读(1054)  评论(4编辑  收藏  举报

导航