SqlServer - Parameterized Query With in Clause C# - CodeProject
SqlServer - Parameterized Query With in Clause C# - CodeProject
A utility class to send parameters for IN() operator in SQL using parameterized queries
InClauseParam.zip - 7.2 KB
Introduction
Using parameterized queries is simple
But things get different when we have to work with IN() clause especially with an unknown number of objects or a list.
https://www.codeproject.com/Tips/1276827/SqlServer-Parameterized-Query-With-IN-Clause-Cshar?display=Print 1/5
14/02/2019 SqlServer: Parameterized Query With IN() Clause C# - CodeProject
listString += ", ";
}
listString += String.Format("{0}{1}", ParamIndicator, Name(i));
}
return listString;
}
ParamsString() will create parameter names string which will be added inside IN()
Params() will provide all the SqlPramter list for SQL command
We call also pass additional or existing SqlPramter's to Params()
/*IN() params*/
SqlServerInClauseParam<string> emailParam = new SqlServerInClauseParam<string>(SqlDbType.VarChar,
emails, "email"); /*IN() clause param*/
SqlServerInClauseParam<int> userTypeParam = new SqlServerInClauseParam<int>(SqlDbType.Int, userTypes,
"userType"); /*IN() clause param*/
/*regular param*/
SqlParameter isActiveParam = new SqlParameter("isActiveParam", SqlDbType.Bit) { Value = isActive };
/*regular param*/
/*sql*/
string sql = String.Format(@"
SELECT *
FROM Employee
WHERE Email IN ({0})
https://www.codeproject.com/Tips/1276827/SqlServer-Parameterized-Query-With-IN-Clause-Cshar?display=Print 2/5
14/02/2019 SqlServer: Parameterized Query With IN() Clause C# - CodeProject
OR UserType IN ({1})
AND IsActive = @isActiveParam;",
emailParam.ParamsString(), userTypeParam.ParamsString() /*using IN() clause param class*/
);
Data
https://www.codeproject.com/Tips/1276827/SqlServer-Parameterized-Query-With-IN-Clause-Cshar?display=Print 3/5
14/02/2019 SqlServer: Parameterized Query With IN() Clause C# - CodeProject
USE [Ums]
GO
/****** Object: Table [dbo].[Employee] Script Date: 2/10/2019 1:01:34 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Employee](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](100) NULL,
[Email] [varchar](100) NULL,
[UserType] [int] NULL,
[IsActive] [bit] NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Employee] ON
GO
INSERT [dbo].[Employee] ([Id], [Name], [Email], [UserType], [IsActive]) VALUES (1, N'Jeff',
N'[email protected]', 1, 1)
GO
INSERT [dbo].[Employee] ([Id], [Name], [Email], [UserType], [IsActive]) VALUES (2, N'Tom',
N'[email protected]', 2, 1)
GO
INSERT [dbo].[Employee] ([Id], [Name], [Email], [UserType], [IsActive]) VALUES (3, N'Dan',
N'[email protected]', 3, 1)
GO
INSERT [dbo].[Employee] ([Id], [Name], [Email], [UserType], [IsActive]) VALUES (4, N'Ban',
N'[email protected]', 4, 1)
GO
SET IDENTITY_INSERT [dbo].[Employee] OFF
GO
Db Connection String
Change the DB connection at App.config as needed
<connectionStrings>
<add name="UmsDbContext" connectionString="Server=L-156151377\SQLEXPRESS;Database=Ums;user
id=sa;password=pro@123;Integrated Security=false;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Other Databases
If we need to do the same for other Databases, we only have to introduce a few modifications at
https://www.codeproject.com/Tips/1276827/SqlServer-Parameterized-Query-With-IN-Clause-Cshar?display=Print 4/5
14/02/2019 SqlServer: Parameterized Query With IN() Clause C# - CodeProject
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2019 by DiponRoy
Web01 | 2.8.190210.1 | Last Updated 14 Feb 2019 Everything else Copyright © CodeProject, 1999-2019
https://www.codeproject.com/Tips/1276827/SqlServer-Parameterized-Query-With-IN-Clause-Cshar?display=Print 5/5