본문 바로가기

Programming/C#22

[C#] SqlParameterCollection 에러 The SqlParameter is already contained by another SqlParameterCollection SqlParameter이(가) 이미 다른 SqlParameterCollection에 들어 있습니다. 위와 같이 DB SQL 실행을 하는 method를 2번 호출하면 볼 수 있는 에러다. using 문이 끝나서 SqlCommand가 Dispose() 되어도 SqlParameter가 어떤 SqlCommand에서 사용되었는지 정보를 저장하고 있는 것 같다. SqlCommand.Parameters.Clear() method를 이용해서 해당 SqlCommand의 SQL parameter를 초기화해서 에러를 방지할 수 있다. command.Parameters.Clear(); 아래 소스코드.. 2021. 6. 14.
[C#] IsNullOrEmpty vs IsNullOrWhiteSpace C#에서 빈 문자열 또는 null인 경우를 확인할 때 사용하는 Method의 차이를 알아보자. Visual Studio C# Interactive 기능을 이용해서 확인해봤다. IsNullOrWhiteSpace()의 경우, space 또는 tab, line break도 true를 리턴한다. 생각해보니 실제로 제품에서 사용할때 IsNullOrEmpty()로 충분할 것 같다. extension method로 someStringVariable.IsNullOrEmpty() 형식으로 사용하는 방법도 사용하기도 편하고 의미상으로도 좋을 것 같다. Reference https://stackoverflow.com/questions/18710644/difference-between-isnullorempty-and-isnu.. 2021. 6. 2.
[c#] Lambda 표현식을 파라미터로 받기 Method에서 Lambda Expression을 파라미터로 받기 위해서 두 가지 방법이 있다. 1. Expression 2. Func, Func, ... public class Program { public static void Main() { Console.WriteLine(Method1(x => x + " add string")); Console.WriteLine(Method2(x => x + " add string")); } public static string Method1(Expression expression) { string x = "Method1"; var method = expression.Compile(); return method(x); } public static string Me.. 2020. 11. 10.
[c#] DateTime class 이용해서 날짜 가져오기 DateTime 클래스를 이용해서 현재 날짜를 가져와 보도록하겠습니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 using System; public class Program { public static void Main() { var today = DateTime.Today; var day = DateTime.Now; var firstDayOfMonth = new DateTime(today.Year, today.Month, 1); var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1); Console.WriteLine(today + " , " + day); Console.WriteLine(firstDayOfMonth +.. 2019. 10. 3.