I have been searching for a solution to this and so far found nothing that works despite some others having similar issues. I am running an SQL statement using the following code (sorry about the hideous formatting. That is what we use here):
/// <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;
}
}
The query is as follows (with values substituted in):
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
CONRIBUTO column is a varchar(max)
GEOMETRY column is a geometry data type
When I run the statement directly on the database it runs without error and returns what I expect.
When I run it via the C# however using a prepared statement (obtained from a table earlier in the C# code) the error is produced. The parameters that are passed in are as follows:
@5 = -25.43623984375 double
@6 = 44.257784519021 double
@7 = 21.62918984375 double
@8 = 60.752403080295 double
The query without substituted values is as follows:
select CONTRIBUTO, count(*)
from HA_WITHOUT_SCOTLAND
where STIntersects(GEOMETRY, geometry::STGeomFromText('POLYGON(('+@5+' '+@6+', '+@7+' '+@6+', '+@7+' '+@8+', '+@5+' '+@8+', '+@5+' '+@6+'))', 4326)) = 1
group by CONTRIBUTO
I cannot see anywhere in here that would produce this error given the fact that the columns and parameters are the correct data types.
Can anyone shed any light on this?
The problem is down to the section that reads:
This will attempt to convert the text
POLYGON(('into a float so that it matches the@Sparameter (so they can be added).Instead, you could try wrapping each parameter in a
CONVERT, for example:The everything will be a string for the
+operator, which will concatenate them instead.In your substituted version, you’ve already handled the conversion to a varchar yourself, so there’s no problem.