65.9K
CodeProject is changing. Read more.
Home

Get LINQ GetCommand Parameters

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Dec 5, 2012

CPOL
viewsIcon

7861

downloadIcon

42

Get LINQ GetCommand parameters.

Introduction

The article code returns GetCommand parameters for creating a Log In project.

Background

Create a Log in project afther Insert, Update, or Delete records.

Using the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace GetLinQueryLog
{
public class GetParameters
{
    public string CreateSqlLog(SqlCommand command)
    {
        string commandtext = command.CommandText;

        for (int i = command.Parameters.Count - 1; i >= 0; i--)
        {
            if (command.Parameters[i].SqlDbType == System.Data.SqlDbType.VarChar || 
                    command.Parameters[i].SqlDbType == System.Data.SqlDbType.UniqueIdentifier)
            {
                commandtext = commandtext.Replace(command.Parameters[i].ParameterName, 
                       "'" + command.Parameters[i].Value.ToString() + "'");
            }
            else if (command.Parameters[i].SqlDbType == System.Data.SqlDbType.NVarChar)
            {
                commandtext = commandtext.Replace(command.Parameters[i].ParameterName, 
                     " N'" + command.Parameters[i].Value.ToString() + "'");
            }
            else
            {
                commandtext = commandtext.Replace(
                  command.Parameters[i].ParameterName, command.Parameters[i].Value.ToString());
            }
        }
        return commandtext;
    }
}
}