EDIT : Problem with the email I get mailbox unavailable exception! DBEmail.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Xml.Linq;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
{
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
m.From = new System.Net.Mail.MailAddress(Sender);
m.To.Add(new System.Net.Mail.MailAddress(SendTo));
m.Subject = Subject;
m.Body = Body;
System.Net.Mail.SmtpClient client = null;
client = new System.Net.Mail.SmtpClient();
client.Host = mailServer;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a7f594f586a675379435e4f04494547" rel="noreferrer noopener nofollow">[email protected]</a>", "password");
client.Send(m);
}
}
我必须每晚运行一些存储过程。所以我创建了一个 .sql 文件,它基本上 -
exec proc1
exec proc2...
那么,如果我只是像这样运行它们,或者它们需要包含在 begin end 或其他内容中,这样可以吗?此外,我有一个批处理文件,我将安排运行它 -
sqlcmd -S myserver.myserver.com -i
D:\Scripts\StartProcs.sql -U username -P password -o D:\Scripts\log.txt
上面的脚本正确吗?我的意思是它可以运行它进行测试...但我只需要确保我是否在这里错过了任何其他问题?因为我看到其他一些 sqlcmd 命令非常长并且需要很多参数...我只是想确保我没有丢失任何重要参数..
请使用 .sql 文件或批处理文件纠正我上面的错误...如果我没有考虑到任何问题? ...谢谢
最佳答案
我看到的唯一问题是,使用这种技术(除非您在过程中内置了某些内容),如果出现问题,或者在这种情况下成功完成,您将不会自动收到通知。
在 SQL Server Express 中,您可以添加一个 CLR 函数,您可以使用该函数向您发送一封电子邮件,报告上述存储过程的结果。
以下是使用所有免费工具的方法。
首先确保通过执行此命令启用 CLR 集成
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
您还需要对程序集进行签名或将数据库标记为“可信”。要将数据库标记为可信,请确保您以 SysAdmin 角色成员的身份登录 SQL Server 并执行此操作
ALTER DATABASE YourDBName SET TRUSTWORTHY ON;
接下来将以下内容保存为 C:\Code\DBEmail.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
{
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
m.From = new System.Net.Mail.MailAddress(Sender);
m.To.Add(new System.Net.Mail.MailAddress(SendTo));
m.Subject = Subject;
m.Body = Body;
System.Net.Mail.SmtpClient client = null;
client = new System.Net.Mail.SmtpClient();
client.Host = mailServer;
client.Send(m);
}
}
然后在相应的 .Net Framework 目录中找到 C# 命令行编译器,在我的例子中是“C:\Windows\Microsoft.NET\Framework\v2.0.50727”)cd 到它并执行它。
csc.exe /target:library /out:C:\Code\DBEmail.dll C:\Code\DBEmail.cs
然后将生成的 C:\Code\DBEmail.dll 复制到 SQL Server(如果尚未使用)并在 Management studio 中执行此操作。
CREATE ASSEMBLY [DBEmail]
AUTHORIZATION [dbo]
From 'C:\Code\DBEmail.dll'
WITH PERMISSION_SET = External_Access
最后通过执行此命令创建一个存储过程来映射到 CLR 代码
CREATE PROCEDURE [dbo].[usp_DBEmail]
@Sender [nvarchar](255),
@SendTo [nvarchar](255),
@Subject [nvarchar](255),
@Body [nvarchar](max),
@mailserver [nvarchar](55)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [DBEmail].[StoredProcedures].[DBEmail]
然后,您可以将代码包装在 Try Catch 中的预定存储过程中,如果发生错误,请在电子邮件中向自己发送错误消息,如下所示。
BEGIN TRY
--Your SQL Stuff
END TRY
BEGIN CATCH
Declare @error as nvarchar(max);
SELECT @error = ERROR_NUMBER();
Exec usp_DBEmail @Sender='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e89b8d868c8d9aa8909192c68b8785" rel="noreferrer noopener nofollow">[email protected]</a>', @SendTo='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0d9cfd5e0d8d9da8ec3cfcd" rel="noreferrer noopener nofollow">[email protected]</a>', @subject='Whoops', @Body=@error, @mailserver='smtp_ip'
END CATCH;
希望这对您有帮助!
关于sql - sql server 2008 中的计划程序我的做法正确还是存在问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3310335/