Linq DataContext.Log-使用参数记录SQL命令 [英] Linq DataContext.Log - logging sql command with parameters

查看:105
本文介绍了Linq DataContext.Log-使用参数记录SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Linq DataContext.Log,并且想保存带有参数的sql命令.我该怎么办??

I using Linq DataContext.Log and I want to save sql command with parameters. how may I do this??

现在要写日志:

SELECT [t0].[Id_User],
[t0].[FirstName], [t0].[LastName],
[t0].[UserName], [t0].[Password],
[t0].[District_Id], [t0].[Active],
[t0].[MobileDevice_Id],
[t0].[IsMobile], [t0].[IsWWW],
[t0].[IsWholesaler], [t0].[Acc_Admin],
[t0].[Warehouse_Id], [t0].[PIN],
[t0].[ValidFrom], [t0].[ValidTo],
[t0].[IsExternal], [t0].[UserType],
[t0].[DefaultDepartment_Id],
[t0].[Code], [t0].[RowsOnPage],
[t0].[ClientGroup_Id],
[t0].[ClientGroup2_Id],
[t0].[ServerHash],
[t0].[CanOrderInPacks], [t0].[Email],
[t0].[IsAdmin],
[t0].[HasAccessToAllInferiorsData],
[t0].[IsSupplier], [t0].[Position],
[t0].[syncstamp] AS [Syncstamp],
[t0].[Source], [t0].[Deleted],
[t0].[DefaultClient_Id] FROM
[dbo].[Users] AS [t0] WHERE
([t0].[UserName] = @p0) AND
([t0].[Deleted] = @p1)

我要写@p0 and @p1进行记录

推荐答案

可以通过下面的代码行

var results = db.calltodatabase.Where(pradicate); 
IQueryable query = results;

    DbCommand dbCommand = dataContext.GetCommand(query);


        var result = new SqlQueryText();


        result.Text = dbCommand.CommandText;
        int nParams = dbCommand.Parameters.Count;
        result.Params = new ParameterText[nParams];
        for (int j = 0; j < nParams; j++)
        {
            var param = new ParameterText();
            DbParameter pInfo = dbCommand.Parameters[j];
            param.Name = pInfo.ParameterName;
            param.SqlType = pInfo.DbType.ToString();
            object paramValue = pInfo.Value;
            if (paramValue == null)
            {
                param.Value = null;
            }
            else
            {
                param.Value = pInfo.Value.ToString();
            }
            result.Params[j] = param;
        }

这篇关于Linq DataContext.Log-使用参数记录SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆