Reputation: 85
Code to refer
[WebMethod]
public static string GetData()
{
string query = "SELECT * FROM tblCountry";
SqlCommand cmd = new SqlCommand(query);
return GetData(cmd).GetXml();
}
private static DataSet GetData(SqlCommand cmd)
{
string strConnString = @"Data Source=.\sqlExpress;Initial Catalog=dbTest;Integrated Security=SSPI; pooling=false";
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
See more at: Link
The query returns 20 rows and i need to show ten rows alone. Without any alteration in query is there any possibility to limit the data in dataset.Suggest some ideas
Upvotes: 3
Views: 7528
Reputation: 1023
string query = "SELECT TOP 10 FROM tblCountry";
You can make a query like this, to return only 10 rows.
Upvotes: -2
Reputation: 7505
You can use an overload of Fill
that takes the start and end record index.
var ds = new DataSet; //using means the DataSet will go out of scope so you can't return it!
sda.Fill(1, 10, ds.Tables.Add("MyTable"));
return ds; //it now contains a table called "MyTable".
You can find more details here
But like most commenters here, I'd be much more in favour of modifying the query if at all possible
Upvotes: 1
Reputation: 311
How strict is your requirement to not alter the query? Can you prepend some text to the query?
If so, you can use SET ROWCOUNT.
Example:
string query = "SET ROWCOUNT 10;"
query += "SELECT * FROM tblCountry";
Upvotes: 0
Reputation: 40990
You can try this
var rows = ds.Tables[0].AsEnumerable().Take(10);
Now you can also convert these rows into a datatable like this
DataTable limitedTable = rows.CopyToDataTable<DataRow>();
Upvotes: 5