Iga*_*hne 8 c# sql sql-server entity-framework-6
我被要求在我的工作中为系统开发审计.该系统已经完成.我认为EF 6的命令拦截应该适合我的目的.
但是,在这种情况下我们想要知道谁发送了请假,我们希望能够拦截这个插入查询.
using (DataContext context = new DataContext())
{
var result = context.CreateLeavePrerequest(
leaveRequest.LeaveType,
leaveRequest.StartDate,
leaveRequest.EndDate,
leaveRequest.NumberOfDays,
leaveRequest.EmployeeComment,
leaveRequest.HasSupportingDocumentation,
leaveRequest.ResourceTag,
leaveRequest.RemainingBalance,
leaveRequest.ApproverResourceTag,
leaveRequest.CapturerResourceTag,
leaveRequest.SupportingDocumentID,
ref id
);
Run Code Online (Sandbox Code Playgroud)
那么存储过程是:
CREATE PROCEDURE [dbo].[CreateLeavePrerequest]
(
@LeaveType VARCHAR(50) ,
@StartDate DATETIME ,
@EndDate DATETIME ,
@NumberOfDays DECIMAL(18, 5) ,
@EmployeeComment VARCHAR(512) ,
@SickNoteIndicator BIT ,
@ResourceTag INT,
@RemainingBalance DECIMAL,
@ApproverResourceTag INT,
@CapturerResourceTag INT,
@SupportingDocumentID INT,
@id INT = 0 OUT
)
AS
BEGIN
INSERT INTO [ESS PER LVE PreRequest]
( [Resource Tag] ,
[Leave Type] ,
[Start Date] ,
[End Date] ,
[No Of Days] ,
[Employee Comments] ,
[Sick Note Indicator],
[Status],
[Remaining Balance],
[Approver Resource Tag],
[Capturer Resource Tag],
[SupportingDocumentID]
)
SELECT @ResourceTag ,
@LeaveType ,
@StartDate ,
@EndDate ,
@NumberOfDays ,
@EmployeeComment ,
@SickNoteIndicator,
'Captured',
@RemainingBalance,
@ApproverResourceTag,
@CapturerResourceTag,
@SupportingDocumentID;
SELECT @id
END
Run Code Online (Sandbox Code Playgroud)
更新:
CreateLeavePrerequest实现如下:
public ISingleResult<CreateLeavePrerequestResult> CreateLeavePrerequest([global::System.Data.Linq.Mapping.ParameterAttribute(Name="LeaveType", DbType="VarChar(50)")] string leaveType, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="StartDate", DbType="DateTime")] System.Nullable<System.DateTime> startDate, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="EndDate", DbType="DateTime")] System.Nullable<System.DateTime> endDate, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="NumberOfDays", DbType="Decimal(18,5)")] System.Nullable<decimal> numberOfDays, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="EmployeeComment", DbType="VarChar(512)")] string employeeComment, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="SickNoteIndicator", DbType="Bit")] System.Nullable<bool> sickNoteIndicator, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="ResourceTag", DbType="Int")] System.Nullable<int> resourceTag, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="RemainingBalance", DbType="Decimal(18,0)")] System.Nullable<decimal> remainingBalance, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="ApproverResourceTag", DbType="Int")] System.Nullable<int> approverResourceTag, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="CapturerResourceTag", DbType="Int")] System.Nullable<int> capturerResourceTag, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="SupportingDocumentID", DbType="Int")] System.Nullable<int> supportingDocumentID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] ref System.Nullable<int> id)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), leaveType, startDate, endDate, numberOfDays, employeeComment, sickNoteIndicator, resourceTag, remainingBalance, approverResourceTag, capturerResourceTag, supportingDocumentID, id);
id = ((System.Nullable<int>)(result.GetParameterValue(11)));
return ((ISingleResult<CreateLeavePrerequestResult>)(result.ReturnValue));
}
Run Code Online (Sandbox Code Playgroud)
更新2
Global.asax中的DBCommandInterceptor注册:
protected void Application_Start()
{
DbInterception.Add(new Auditor());
}
Run Code Online (Sandbox Code Playgroud)
DBCommandInterceptor实现:
我快速实现了这一点,以便我可以看到是否可以拦截任何东西,所以它只是写入Debug窗口.我已经能够拦截一些Select查询,但这不是我们想要审核的.
public class Auditor : IDbCommandInterceptor
{
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
CreateAuditMessage(command, interceptionContext);
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
CreateAuditMessage(command, interceptionContext);
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
CreateAuditMessage(command, interceptionContext);
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
CreateAuditMessage(command, interceptionContext);
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
CreateAuditMessage(command, interceptionContext);
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
CreateAuditMessage(command, interceptionContext);
}
public static void CreateAuditMessage<T>(DbCommand command, DbCommandInterceptionContext<T> interceptionContext)
{
string message;
var parameters = new StringBuilder();
foreach (DbParameter param in command.Parameters)
{
parameters.AppendLine(param.ParameterName + " " + param.DbType + " = " + param.Value);
}
if (interceptionContext.Exception == null)
{
message = (parameters.ToString() + " " + command.CommandText);
}
else
{
message = (parameters.ToString() + command.CommandText + " " + interceptionContext.Exception);
}
Debug.WriteLine(message);
}
}
Run Code Online (Sandbox Code Playgroud)
最近,我一直在阅读很多关于实体框架的内容,但我不是很了解.我已经实现了IDbCommandInterface并注册了它等等.我能够看到其他一些查询被截获,但由于上述情况导致存储过程被称为"外部",我无法获取参数.
这是一个简单的例子.并非所有在系统中以类似方式调用的存储过程都非常简单.
改变上述情况的最佳方法是什么,以便我们可以应用拦截,从而应用审计?
我更喜欢下面的方法从我的应用程序执行 SQL 存储过程,因为我也使用它从远程服务器执行存储过程。您唯一需要注意的是将正确格式化的参数传递给相应的参数类型。
using (SqlConnection con = new SqlConnection(dc.Con)) {
using (SqlCommand cmd = new SqlCommand("CreateLeavePrerequest", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LeaveType", SqlDbType.VarChar).Value = leaveType;
cmd.Parameters.Add("@StartDate", SqlDbType.VarChar).Value = startDate;
cmd.Parameters.Add("@EndDate", SqlDbType.VarChar).Value = endDate;
cmd.Parameters.Add("@NumberOfDays", SqlDbType.VarChar).Value = numberOfDays;
cmd.Parameters.Add("@EmployeeComment", SqlDbType.VarChar).Value = employeeComment;
cmd.Parameters.Add("@SickNoteIndicator", SqlDbType.VarChar).Value = sickNoteIndicator;
cmd.Parameters.Add("@ResourceTag", SqlDbType.VarChar).Value = resourceTag;
cmd.Parameters.Add("@RemainingBalance", SqlDbType.VarChar).Value = remainingBalance;
cmd.Parameters.Add("@ApproverResourceTag", SqlDbType.VarChar).Value = approverResourceTag;
cmd.Parameters.Add("@CapturerResourceTag", SqlDbType.VarChar).Value = capturerResourceTag;
cmd.Parameters.Add("@SupportingDocumentID", SqlDbType.VarChar).Value = supportingDocumentID;
cmd.Parameters["@id"].Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
对于任何 NULL 值检查
DBNull.Value
Run Code Online (Sandbox Code Playgroud)
让我知道这是否有帮助。谢谢!
| 归档时间: |
|
| 查看次数: |
330 次 |
| 最近记录: |