将数据类型varchar转换为float时出错。准备声明

时间:2012-06-07 08:13:03

标签: c# sql-server-2008

我一直在寻找解决方案,到目前为止,尽管其他一些人遇到类似的问题,但没有发现任何有效的方法。我正在使用以下代码运行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

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

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

1 个答案:

答案 0 :(得分:1)

问题在于以下部分:

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

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

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

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

所有内容都是+运算符的字符串,它将连接它们。

在您的替换版本中,您已经自己处理了转换为varchar,因此没有问题。