2012-06-07 3 views
1

나는 이것에 대한 해결책을 찾고 있었고 지금까지 비슷한 문제가있는 다른 것들에도 불구하고 아무 것도 발견하지 못했다. 나는 다음과 같은 코드를 사용하여 SQL 문을 실행하고 (무시 무시한 형식에 대한 유감을 즉, 우리가 여기에 사용하는 것입니다.) :데이터 유형 varchar를 float로 변환하는 중 오류가 발생했습니다. Prepare statement

select CONTRIBUTO, count(*) as CountCol 
from HA_WITHOUT_SCOTLAND 
where 
GEOMETRY.STIntersects(geometry::STGeomFromText('POLYGON((-25.43623984375 44.257784519021, 21.62918984375 44.257784519021, 21.62918984375 60.752403080295, -25.43623984375 60.752403080295, -25.43623984375 44.257784519021))', 4326)) = 1 
group by CONTRIBUTO 
다음과 같이

/// <summary> 
    /// Executes a prepared statement with the parameters passed to AddParameter(parameterName, parameterValue) and creates a recordset. 
    /// </summary> 
    /// 
    /// <param name="sqlQuery">The sql statement to execute.</param> 
    public DbStandardResponseType ExecutePreparedStatementWithParametersQuery (string sqlQuery) 
     { 
     DbStandardResponseType dbFactoryResponse = new DbStandardResponseType(); 

     if (String.IsNullOrEmpty (sqlQuery)) 
      { 
      dbFactoryResponse.ExceptionMessage = "No query string passed."; 
      dbFactoryResponse.Success = false; 
      dbFactoryResponse.UserFriendlyMessage = "No query string passed."; 

      return dbFactoryResponse; 
      } 

     try 
      { 
      //attempt to prepare our connection 
      dbFactoryResponse = PrepareConnection(); 

      if (!dbFactoryResponse.Success) 
       { 
       return dbFactoryResponse; 
       } 

      m_dbFactoryDatabaseCommand.CommandText = sqlQuery; 
      m_dbFactoryDatabaseCommand.CommandType = CommandType.Text; 

      if (m_parameterName.Count != 0) 
       { 
       for (int i = 0; i < m_parameterName.Count; i++) 
        { 
        //create a new parameter object and assign its values before adding it to the connection object 
        DbParameter parameter = m_dbFactoryDatabaseCommand.CreateParameter(); 
        parameter.Value = m_parameterValue[i]; 
        parameter.ParameterName = m_parameterName[i]; 
        m_dbFactoryDatabaseCommand.Parameters.Add (parameter); 
        } 

       m_parameterName.Clear(); 
       m_parameterValue.Clear(); 
       } 

      m_hasRecordSet = true; 

      *****Error appears on this line inside the reader object***** 

      m_dbFactoryDatabaseDataReader = m_dbFactoryDatabaseCommand.ExecuteReader(); 

      dbFactoryResponse.ExceptionMessage = ""; 
      dbFactoryResponse.Success = true; 
      dbFactoryResponse.UserFriendlyMessage = "OK"; 

      return dbFactoryResponse; 
      } 
     catch (Exception ex) 
      { 
      if (m_queueErrors) 
       { 
       m_queuedErrorsList.AppendLine (ex.Message); 
       m_queuedErrorsList.AppendLine ("\r\n\r\nPrepared Statement: " + sqlQuery); 
       m_queuedErrorsList.AppendLine(); 

       m_queuedErrorCount++; 

       dbFactoryResponse.ExceptionMessage = m_queuedErrorsList.ToString(); 
       dbFactoryResponse.Success = false; 
       dbFactoryResponse.UserFriendlyMessage = "Execute failed on the database."; 
       } 
      else 
       { 
       dbFactoryResponse.ExceptionMessage = ex.Message; 
       dbFactoryResponse.Success = false; 
       dbFactoryResponse.UserFriendlyMessage = "Execute failed on the database."; 
       } 

      try 
       { 
       Close(); 
       } 
      catch (Exception f) 
       { 
       dbFactoryResponse.ExceptionMessage = f.Message + "\r\n\r\nPrepared Statement: " + sqlQuery; 
       dbFactoryResponse.Success = false; 
       dbFactoryResponse.UserFriendlyMessage = "Failed to close the connection to the database."; 
       } 

      return dbFactoryResponse; 
      } 
     } 

쿼리는 (에서 대체 값)

CONRIBUTO 열이 varchar (최대) GEOMETRY 열이 기하학 데이터 형식입니다.

데이터베이스에서 직접 문을 실행하면 오류없이 실행되고 예상 한 결과가 반환됩니다.

그러나 C# 코드를 사용하여 준비된 문을 사용하여 C#을 실행하면 오류가 발생합니다. 다음과 같이 대체 값없이

@5 = -25.43623984375 double 
@6 = 44.257784519021 double 
@7 = 21.62918984375 double 
@8 = 60.752403080295 double 

쿼리는 다음과 같습니다 : 다음과 같이 전달되는 매개 변수는

select CONTRIBUTO, count(*) 
from HA_WITHOUT_SCOTLAND 
where STIntersects(GEOMETRY, geometry::STGeomFromText('POLYGON(('[email protected]+' '[email protected]+', '[email protected]+' '[email protected]+', '[email protected]+' '[email protected]+', '[email protected]+' '[email protected]+', '[email protected]+' '[email protected]+'))', 4326)) = 1 
group by CONTRIBUTO 

나는 사실을 감안할 때이 오류가 발생 것이라고 여기에 어디서나 볼 수없는 열 매개 변수는 올바른 데이터 유형입니다.

누구든지이 문제에 관해 밝힐 수 있습니까?

+0

@ V4Vendetta :'geometry_tagged_text' 매개 변수는 nvarchar (max)입니다 (http://msdn.microsoft.com/en-us/library/bb933823.aspx 참조) –

답변

1

문제는 읽는 부분까지이다 : 그것은 @S 매개 변수를 일치하도록이 부동 소수점으로 텍스트 POLYGON((' 변환하려고 시도합니다 (그래서 그들은 추가 할 수 있습니다)

'POLYGON(('[email protected]+'... 

.

대신, 예를 들어, CONVERT에서 각 매개 변수를 포장 시도 할 수 :

'POLYGON(('+CONVERT(VARCHAR(100),@5)+'... 

모든 대신을 연결할 것이다 + 연산자에 대한 문자열이 될 것입니다.

대체 버전에서는 이미 varchar 로의 변환을 처리 했으므로 아무런 문제가 없습니다.

+0

잘 모르겠지만 대체 할 수는 없습니다. '@ 5'에 값을 넣었 기 때문에 OP가 보여준 것처럼 스스로 쿼리합니다. – V4Vendetta

+1

@s는 문자열이 아닌 이중으로 정의됩니다. –

+0

네, 그렇지만 그것들을 문자열로 만들고 '+'같은 쿼리에 추가하고 끝내지 않을 것입니다. – V4Vendetta

관련 문제