C# 으로 데이터베이스에 쿼리를 실행하려고 합니다.

조회수 668회

C#으로 데이터를 받아 디비에서 데이터를 불러와서 출력하는 프로그램을 짜고 있습니다. 텍스트박스에 조건을 입력하면 그 조건에 맞게 모든 데이터를 불러옵니다. 아래는 그 쿼리문입니다.

query = "DECLARE @workercode varchar(30), @departcode char(20)";
                query += "SET @workercode = " + workercodes + "; SET @departcode = " + departcodes +";";
                query += "SELECT WorkerCd AS '사원코드', DepartCd AS '부서코드', WorkerName AS '성명', WorkerRRN AS '주민등록번호', WorkerAddress AS '주소', WorkerPhonenum AS '전화번호', WorkerMobilepn AS '휴대폰번호', RankCd AS '직위코드', PositionCd AS '직책코드', LevelId AS 'LEVEL ID', LIPassword AS '비밀번호', Startdate AS '입사일자', ResignationDay AS '퇴사일자', WorkerType AS '사원구분'";
                query += "FROM USERINFO WHERE WorkerCd = '@workercode' AND DepartCd = '@departcode';";

이렇게 썼는데 텍스트박스로 데이터를 입력하니까

System.Data.SqlClient.SqlException: '열 이름 '테스트1'이(가) 유효하지 않습니다. 열 이름 '테스트1'이(가) 유효하지 않습니다.'System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException, bool, System.Action) System.Data.SqlClient.SqlInternalConnection.OnError(System.Data.SqlClient.SqlException, bool, System.Action) System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject, bool, bool) System.Data.SqlClient.TdsParser.TryRun(System.Data.SqlClient.RunBehavior, System.Data.SqlClient.SqlCommand, System.Data.SqlClient.SqlDataReader, System.Data.SqlClient.BulkCopySimpleResultSet, System.Data.SqlClient.TdsParserStateObject, out bool) System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() System.Data.SqlClient.SqlDataReader.MetaData.get() System.Data.SqlClient.SqlCommand.FinishExecuteReader(System.Data.SqlClient.SqlDataReader, System.Data.SqlClient.RunBehavior, string, bool, bool, bool) System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(System.Data.CommandBehavior, System.Data.SqlClient.RunBehavior, bool, bool, int, out System.Threading.Tasks.Task, bool, bool, System.Data.SqlClient.SqlDataReader, bool) System.Data.SqlClient.SqlCommand.RunExecuteReader(System.Data.CommandBehavior, System.Data.SqlClient.RunBehavior, bool, string, System.Threading.Tasks.TaskCompletionSource, int, out System.Threading.Tasks.Task, out bool, bool, bool) System.Data.SqlClient.SqlCommand.RunExecuteReader(System.Data.CommandBehavior, System.Data.SqlClient.RunBehavior, bool, string) ... [호출 스택 잘림]

이런 오류메세지가 뜹니다. 아, 테스트1은 제가 텍스트박스로 입력한 값입니다. 입력한 값이 유효하지 않다고 하는데 어떤 면에서 값이 맞지 않는다고 하는건지 모르겠습니다. 참고로 @workercode나 @departcode를 빼고 다른 조건을 넣으니 정상적으로 작동합니다. 왜 그런걸까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)