2010-06-03 4 views
0

SQL Server로 전송되는 SqlCommand 개체를 실제 T-SQL 명령으로 변환하는 방법이 있습니까?SqlCommand to T-SQL

+0

속는 : HTTP : // stackoverflow.com/questions/2611446/what-sql-is-being-sent-from-a-sqlcommand-object – tzaman

답변

0

필자는 그렇게 생각하지 않습니다. 매개 변수와 쿼리 텍스트는 SQL Server에 개별적으로 보내 지므로 그에 따라 SQL Server가 처리합니다. SqlCommand.CommandText에서 쿼리를 가져올 수 있으며 매개 변수는 SqlCommand.Parameters 컬렉션에 저장됩니다. SQL Server로 보내지는 것과는 다른 뭔가를 가지고있을지라도 쿼리를 변환하기 위해서는 문자열 조작을해야합니다.

이 질문은 너무,이에 대한 몇 가지 흥미로운 정보가 있습니다 How does SqlCommand sanitize parameters?

0

실제로이 확장 (잘 작동하지 않는, 단지 생각)처럼 뭔가에 대해 생각했다 :

public static string ToQuery(this SqlCommand command) 
{ 
    StringBuilder sb = new StringBuilder(); 
    sb.Append("exec "); 
    sb.Append(command.CommandText); 

    List<string> parameters = new List<string>(); 

    for (int i=0; i<command.Parameters.Count;i++) 
    { 
    if (command.Parameters[i].Direction != ParameterDirection.ReturnValue) 
    { 
     parameters.Add(string.Format(" {0}={1}", command.Parameters[i].ParameterName, command.Parameters[i].Value)); 
    } 
    } 

    if (parameters.Count > 0) sb.Append(string.Join(",", parameters.ToArray())); 

    return sb.ToString(); 
}