C# 如何将限制定义为参数?

C# 如何将限制定义为参数?,c#,tsql,odbc,C#,Tsql,Odbc,如果conn是一个OdbcConnection对象,count是一个int,我将如何使用count作为查询的参数 ... var query = conn.CreateCommand(); query.CommandText = "select top ? * from players order by Points desc"; query.Parameters.Add("top", OdbcType.Int).Value = count; var reader = query.Execut

如果
conn
是一个
OdbcConnection
对象,
count
是一个
int
,我将如何使用count作为查询的参数

...
var query = conn.CreateCommand();
query.CommandText = "select top ? * from players order by Points desc";
query.Parameters.Add("top", OdbcType.Int).Value = count;

var reader = query.ExecuteReader();
while (reader.Read())
{
    ...
}
...
这样,我在“@P1”附近得到一个语法错误
错误[42000][Microsoft][ODBC SQL Server驱动程序][SQL Server]错误语法。

如果无法以我尝试的方式完成,我将如何以正确的方式完成?

您可以:

query.CommandText = "select top (@topparameter) * from players order by Points desc";
query.Parameters.AddWithValue("@topparameter", count.ToString());
如果您使用的是
SqlServer
,请使用
SqlConnection
SqlCommand
如下:

using (SqlConnection conn = new SqlConnection("YourConnectionString"))
{
    using (SqlCommand query = new SqlCommand("select top (@topparameter) * from players order by Points desc", conn))
    {
        query.Parameters.AddWithValue("@topparameter", count.ToString());
        var reader = query.ExecuteReader();
        while (reader.Read())
        {
        }
    }
}
你可以做:

query.CommandText = "select top (@topparameter) * from players order by Points desc";
query.Parameters.AddWithValue("@topparameter", count.ToString());
如果您使用的是
SqlServer
,请使用
SqlConnection
SqlCommand
如下:

using (SqlConnection conn = new SqlConnection("YourConnectionString"))
{
    using (SqlCommand query = new SqlCommand("select top (@topparameter) * from players order by Points desc", conn))
    {
        query.Parameters.AddWithValue("@topparameter", count.ToString());
        var reader = query.ExecuteReader();
        while (reader.Read())
        {
        }
    }
}

您还可以使用SET ROWCOUNT。优点是可以使用整数作为参数,避免动态查询

SET ROWCOUNT @top;

select * from table;

SET ROWCOUNT 0;

阅读

也可以使用SET ROWCOUNT。优点是可以使用整数作为参数,避免动态查询

SET ROWCOUNT @top;

select * from table;

SET ROWCOUNT 0;

阅读

您不能为commandText串联字符串吗?top不是参数,您应该在查询中包含类似于
@Counter
的内容,并将其作为参数添加。例如,
query.Parameters.Add(“@Counter”,OdbcType.Int).Value=count
@user2525463-您不应该连接字符串,使用参数进行查询是正确的方法,因为它可以防止SQL注入。@DarrenDavies我知道top不是参数,但我如何将参数命名为top的计数?是否可能重复?您不能为commandText连接字符串?top不是参数,查询中应该有类似于
@Counter
的内容,并将其作为参数添加。例如,
query.Parameters.Add(“@Counter”,OdbcType.Int).Value=count
@user2525463-您不应该连接字符串,使用参数进行查询是正确的方法,因为它可以防止SQL注入。@DarrenDavies我知道top不是参数,但如何将参数命名为count for top?这可能会产生
错误[42000][Microsoft][ODBC SQL Server驱动程序][SQL Server]必须声明标量变量“@topparameter”
@matthiaskrull,修改了我关于SQL server的答案,看看它现在是否工作。T-SQL top命令不接受变量作为参数,如果使用动态查询,可以这样做。要避免动态查询,请使用set rowcount,使用下面的我的答案。使用
SqlConnection
它可以这样传递参数。感谢您的帮助这将产生
错误[42000][Microsoft][ODBC SQL Server驱动程序][SQL Server]必须声明标量变量“@topparameter”
@matthiaskrull,修改了我关于SQL server的答案,看看它现在是否工作。T-SQL top命令不接受变量作为参数,如果使用动态查询,可以这样做。要避免动态查询,请使用set rowcount,使用下面的我的答案。使用
SqlConnection
它可以这样传递参数。感谢您的帮助回答。