In order for the
TOP
to work, you need to set the value for the bind variable before executing the statement. Consider the following example
DataTable resultTable = new DataTable();
using (SqlConnection connection = new SqlConnection("Data Source=(local); Initial Catalog=master;Integrated Security=SSPI")) {
using (SqlCommand command = new SqlCommand()) {
command.CommandText = "SELECT TOP (@rowcount) * FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME";
command.Connection = connection;
command.Parameters.AddWithValue("@rowcount", 2);
using (SqlDataAdapter adapter = new SqlDataAdapter(command)) {
connection.Open();
adapter.Fill(resultTable);
connection.Close();
}
}
}
If the result is still not what expected, check if conditions in the
WHERE
clause are eliminating rows.