Click here to Skip to main content
15,887,676 members
Articles / Programming Languages / C#
Tip/Trick

Get LINQ GetCommand Parameters

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 Dec 2012CPOL 7.7K   42   6  
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)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --