将数据类型 varchar 转换为浮点数时出错.准备声明
本文关键字:出错 声明 浮点数 数据类型 varchar 转换 | 更新日期: 2023-09-27 18:36:24
我一直在寻找解决这个问题的方法,到目前为止,尽管其他一些人有类似的问题,但还没有找到任何有效的方法。我正在使用以下代码运行 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)几何列是一种几何数据类型
当我直接在数据库上运行该语句时,它会运行而不会出错并返回我期望的内容。
但是,当我使用预准备语句(从 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
鉴于列和参数是正确的数据类型,我在这里看不到会产生此错误的任何地方。
谁能对此有所了解?
问题在于以下部分:
'POLYGON(('+@5+'...
这将尝试将文本POLYGON(('
转换为浮点数,以便它与@S
参数匹配(以便可以添加它们)。
相反,您可以尝试将每个参数包装在 CONVERT
中,例如:
'POLYGON(('+CONVERT(VARCHAR(100),@5)+'...
所有内容将是 +
运算符的字符串,运算符将连接它们。
在您的替换版本中,您已经自己处理了到 varchar 的转换,所以没有问题。