종종 단일 스레드 응용 프로그램에 대해 아래 패턴을 사용하여 SqlCommands를 만듭니다.준비된 SqlCommand for Webservice
저는 현재 웹 서비스를 만들고 있으며,이 패턴은 동시에 여러 클라이언트의 요청을 처리하지 않을 것이라고 우려하고 있습니다.
단일 클라이언트를 한 번에 실행할 수 있도록 단순히 함수를 잠그는 것 이외의 여러 클라이언트에 대해 "준비된"단일 SqlCommand를 사용하는 방법이 있습니까?
private static SqlCommand cmdInsertRecord;
public static void InsertRecord(String parameter1, String parameter2, SqlConnection connection, SqlTransaction transaction)
{
if (cmdInsertRecord == null)
{
//Create command
cmdInsertRecord = connection.CreateCommand();
cmdInsertRecord.CommandText = @"SQL QUERY";
//Add parameters to command
cmdInsertRecord.Parameters.Add("@Parameter1", SqlDbType.Int);
cmdInsertRecord.Parameters.Add("@Parameter2", SqlDbType.DateTime);
//Prepare the command for use
cmdInsertRecord.Prepare();
}
cmdInsertRecord.Transaction = transaction;
//Note SetParameter is an extension that handles null -> DBNull.
cmdInsertRecord.SetParameter("@Parameter1", parameter1);
cmdInsertRecord.SetParameter("@Parameter2", parameter2);
cmdInsertRecord.ExecuteNonQuery();
}
이 명령은 다른 값으로 여러 번 호출되기 때문에 이러한 작업을 수행하고 싶습니다. 준비된 명령을 작성해야하는 경우와 같습니다. – jzacharuk
@jzacharuk - 글로벌 sqlcommand 객체에서 여전히 이점이 보이지 않습니다. 왜 필요하면 로컬로 선언하고 사용을 끝내면 처리 할 수 있습니까? – JonH