■ IN 조건문을 사용해 조회하는 방법을 보여준다.
▶ SQL Server 테이블
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
CREATE TABLE dbo.Student ( ID INT NOT NULL ,Name NVARCHAR(50) NOT NULL ,CreateTime DATETIME NOT NULL CONSTRAINT PKStudent 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 |
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using System; using System.Data.SqlClient; using System.Linq; using Dapper; ... /// <summary> /// 학생 /// </summary> public class Student { /// <summary> /// ID /// </summary> public int ID { get; set; } /// <summary> /// 성명 /// </summary> public string Name { get; set; } /// <summary> /// 생성 일시 /// </summary> public DateTime CreateTime { get; set; } } ... string connectionString = "Data Source=127.0.0.1;Initial Catalog=TestDB;UID=sa;PWD=1234;Timeout=30"; using(SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "SELECT * FROM dbo.Student WHERE ID in @IDList"; var result = connection.Query(sql, new { IDList = new[] { 1, 2, 3, 4, 5 } }); Console.WriteLine(result.Count()); } |