欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

如何控制用户访问数据库

程序员文章站 2024-02-17 15:08:16
...

使用SQL Server Log On trigger: CREATE DATABASE AuditDb GO USE AuditDb GO /* Create AuditTable */ CREATE TABLE ServerLogonHistory ( SystemUser VARCHAR ( 512 ), DBUser VARCHAR ( 512 ), SPID INT , LogonTime DATETIME ) GO /* Create LogonTrigge

使用SQL Server Log On trigger:

CREATE DATABASE AuditDb

GO

USE AuditDb

GO

/* Create AuditTable */

CREATE TABLE ServerLogonHistory

(SystemUser VARCHAR(512),

DBUser VARCHAR(512),

SPID INT,

LogonTime DATETIME)

GO

/* Create LogonTrigger */

CREATE TRIGGER Tr_ServerLogon

ON ALL SERVER FOR LOGON

AS

BEGIN

INSERT INTO AuditDb.dbo.ServerLogonHistory

SELECT SYSTEM_USER,USER,@@SPID,GETDATE()

END

GO

用户登录后查看监控表:

如何控制用户访问数据库

因为我们可以监控到用户登录SQL Server的情况,所以我们就可以限制用户的访问,参考下面的文章:How to prevent users from accessing SQL Server from any application or any login expect your main application & its login:http://thesqldude.com/2012/04/07/how-to-prevent-users-from-accessing-sql-server-from-any-application-or-any-login-expect-your-main-application-its-login/