2012-08-01 2 views
1

종종 단일 스레드 응용 프로그램에 대해 아래 패턴을 사용하여 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(); 
    } 

답변

2

간단하게 단 하나의 클라이언트가 한 번에 실행할 수 있도록하는 기능을 잠금 이외의 다른 여러 클라이언트에 대한 하나의 "준비"하는 SqlCommand를 사용하는 방법이 있나요?

당신은 왜 - 을 원하지 않으실 겁니까?을 원하십니까?

매번 SqlConnection을 새로 만들고 SqlCommand을 새로 만들어 사용해야합니다. 연결 풀 및 (아마도) 명령문 풀을 효율적으로 처리하도록 처리하십시오.

정적 인 SqlConnection 또는 SqlCommand을 갖는 것은 IMO 문제를 묻는 것입니다.

+0

이 명령은 다른 값으로 여러 번 호출되기 때문에 이러한 작업을 수행하고 싶습니다. 준비된 명령을 작성해야하는 경우와 같습니다. – jzacharuk

+0

@jzacharuk - 글로벌 sqlcommand 객체에서 여전히 이점이 보이지 않습니다. 왜 필요하면 로컬로 선언하고 사용을 끝내면 처리 할 수 ​​있습니까? – JonH