1

我一直在寻找解决方案,但到目前为止,尽管其他一些人有类似的问题,但没有发现任何可行的方法。我正在使用以下代码运行 SQL 语句(对可怕的格式感到抱歉。这就是我们在这里使用的):

    /// <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;
            }
        }

查询如下(用替换的值):

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 列是 varchar(max) 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(('+@5+' '+@6+', '+@7+' '+@6+', '+@7+' '+@8+', '+@5+' '+@8+', '+@5+' '+@6+'))', 4326)) = 1 
group by CONTRIBUTO

鉴于列和参数是正确的数据类型,我在这里看不到任何会产生此错误的地方。

任何人都可以对此有所了解吗?

4

1 回答 1

1

问题归结为以下部分:

'POLYGON(('+@5+'...

这将尝试将文本POLYGON(('转换为浮点数,以使其与@S参数匹配(以便可以添加它们)。

相反,您可以尝试将每个参数包装在 aCONVERT中,例如:

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

一切都将是+运算符的字符串,它将连接它们。

在您的替换版本中,您已经自己处理了到 varchar 的转换,所以没有问题。

于 2012-06-07T08:19:28.560 回答