Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Get LINQ GetCommand Parameters

0.00/5 (No votes)
5 Dec 2012CPOL 7.9K   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

C#
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;
    }
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)