sqlserver在启动时出发什么事件
我在sqlserver中写了一个socket通信的服务端dll,create ASSEMBLY 注册到数据库了,因为是服务端,所以要在数据库开启的时候调用一下,应该在什么事件中写 数据库开启的时候都触发什么事件 sqlserver 数据库 socket 通信
[解决办法]
use master
go
--在数据库启动时,会自动调用这个存储过程,这个clr存储过程已注册
exec sp_procoption '存储过程名','startup','on'
你在存储过程中,比如你是用c#写的,
你可以用c#代码去windows事件日志中去读取信息,然后记录到文件,或者表中。
c#代码,可以参考:http://www.cnblogs.com/luck0235/articles/834028.html
测试环境:.Net Framework 2.0、Windows Server 2003 SP2、Visual Studio 2005 SP1
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Diagnostics;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Windows日志有:"Application"应用程序, "Security"安全, "System"系统
string[] logs = new string[] { "Application", "System" };
StringBuilder result = new StringBuilder();
foreach (string log in logs)
{
EventLog myLog = new EventLog();
myLog.Log = log;
//myLog.MachineName = "rondi-agt0qf9op";
foreach (EventLogEntry entry in myLog.Entries)
{
//EventLogEntryType枚举包括:
//Error 错误事件。
//FailureAudit 失败审核事件。
//Information 信息事件。
//SuccessAudit 成功审核事件。
//Warning 警告事件。
if (entry.EntryType == EventLogEntryType.Error
[解决办法]
entry.EntryType == EventLogEntryType.Warning)
{
result.Append("<font color='red'>" + log);
result.Append(entry.EntryType.ToString() + "</font>");
result.Append("<font color='blue'>(" + entry.TimeWritten.ToString() + ")</font>:");
result.Append(entry.Message + "<br /><br />");
}
}
}
Response.Write(result.ToString());
}
}
[解决办法]
你也可以通过,在你的存储过程中写下面的代码,或者在你的clr存储过程中,再调用另一个存储过程,里面可以写如下的代码,就可以知道当前SQL Server的状态了,然后再触发其他事件:
--显示SQL Server的服务名称:MSSQLSERVER
select @@servicename
--查询某个服务的状态,返回CurrentServiceState列:Running.
--这样就表示正在运行
EXEC master.dbo.xp_servicecontrol
N'QUERYSTATE', --查询状态
N'MSSQLSERVER' --改写为你的SQL Server的服务名称