要引用的代码
[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;
}
}
}
} 请参阅更多信息:链接
查询返回20行,我只需要显示10行。在不改变查询的情况下,是否有可能限制dataset.Suggest中的数据
发布于 2014-01-31 10:20:55
您可以使用过载的Fill,它接受开始和结束记录索引。
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".你可以找到更多的细节这里
但是像这里的大多数评论者一样,如果可能的话,我会更赞成修改查询。
发布于 2014-01-31 10:00:44
你可以试试这个
var rows = ds.Tables[0].AsEnumerable().Take(10);现在,您还可以将这些行转换为如下所示的datatable
DataTable limitedTable = rows.CopyToDataTable<DataRow>();发布于 2014-01-31 10:18:34
您对不更改查询的要求有多严格?您能为查询添加一些文本吗?
如果是这样,您可以使用设置ROWCOUNT。
示例:
string query = "SET ROWCOUNT 10;"
query += "SELECT * FROM tblCountry";https://stackoverflow.com/questions/21477071
复制相似问题