C# SqlCommand.Parameters.Add不替换到命令文本中
我已经尝试了我能想到的一切,但这个简单的C代码不起作用。结果是参数没有被替换,因此查询结果中没有数据C# SqlCommand.Parameters.Add不替换到命令文本中,c#,sql,C#,Sql,我已经尝试了我能想到的一切,但这个简单的C代码不起作用。结果是参数没有被替换,因此查询结果中没有数据 string query = @"select TOP 20 [ID], [NAME], LATITUDE, LONGITUDE, ADDRESS, FEATURES, [DATE UPDATED], ABS(ABS(LATITUDE)- @Lat1 ) + ABS(ABS(LONGITUDE)- @Lng1 ) as diff
string query = @"select TOP 20 [ID], [NAME], LATITUDE, LONGITUDE, ADDRESS, FEATURES, [DATE UPDATED],
ABS(ABS(LATITUDE)- @Lat1 ) + ABS(ABS(LONGITUDE)- @Lng1 ) as diff
from Facility f
where (ABS(ABS(LATITUDE)- @Lat2 ) + ABS(ABS(LONGITUDE)- @Lng2 ) < 2)
order by 8";
using (SqlConnection conn = new SqlConnection())
{
// Create the connectionString
// Trusted_Connection is used to denote the connection uses Windows Authentication
conn.ConnectionString = "Server=ERICS_TOSHIBA\\WEAVSQL;Database=WellBe;Trusted_Connection=true";
conn.Open();
// Create the command
SqlCommand command = new SqlCommand(query, conn);
// Add the parameters.
command.Parameters.Add(new SqlParameter("@Lat1", SqlDbType.Float));
command.Parameters["@Lat1"].Value = dHomeLat;
command.Parameters.Add(new SqlParameter("@Lat2", SqlDbType.Float));
command.Parameters["@Lat2"].Value = dHomeLat;
command.Parameters.Add(new SqlParameter("@Lng1", SqlDbType.Float));
command.Parameters["@Lng1"].Value = dHomeLng;
command.Parameters.Add(new SqlParameter("@Lng2", SqlDbType.Float));
command.Parameters["@Lng2"].Value = dHomeLng;
Console.WriteLine("QUERY IS: \n\n {0}", command.CommandText);
}
此WriteLine的输出如下:
select TOP 20 [ID], [NAME], LATITUDE, LONGITUDE, ADDRESS, FEATURES, [DATE UPDATED],
ABS(ABS(LATITUDE)- @Lat1 ) + ABS(ABS(LONGITUDE)- @Lng1 ) as diff
from Facility f
where (ABS(ABS(LATITUDE)- @Lat2 ) + ABS(ABS(LONGITUDE)- @Lng2 ) < 2)
order by 8
为什么参数值不能替换?我试过6种不同版本的语法。上面并没有列出所有代码。包括dHomeLat和dHomeLng在内的所有变量都有有效值。参数不会替换为CommandText;它们与命令一起发送到服务器,这将启用查询计划缓存之类的功能
您实际上没有问题。参数不会被替换为CommandText;它们与命令一起发送到服务器,这将启用查询计划缓存之类的功能
实际上您没有问题。参数永远不会替换查询字符串。它将实际值与它分开发送。在服务器上运行探查器时,您将看到它们。但您所看到的是预期的结果。参数永远不会替换查询字符串。它将实际值与它分开发送。在服务器上运行探查器时,您将看到它们。但你所说的是预期的结果。谢谢所以我猜查询结果是0行。但是,当我在SQL Management Studio中运行相同的查询时,我会得到结果。所以这个查询肯定还有其他问题。谢谢所以我猜查询结果是0行。但是,当我在SQL Management Studio中运行相同的查询时,我会得到结果。所以这个查询肯定还有其他问题。