C# 如何将表的特定列添加到数据集?
我有一个表TBL_Person,有五列:C# 如何将表的特定列添加到数据集?,c#,dataset,C#,Dataset,我有一个表TBL_Person,有五列: Person_ID, FirstName, LastName, Age, Location 我有一个返回数据集的方法: public DataSet GetPerson() { SqlCommand _select = new SqlCommand(); _select.CommandText = "SP-GetAllPerson"; _select.CommandType = System.Data.CommandType.
Person_ID, FirstName, LastName, Age, Location
我有一个返回数据集的方法:
public DataSet GetPerson()
{
SqlCommand _select = new SqlCommand();
_select.CommandText = "SP-GetAllPerson";
_select.CommandType = System.Data.CommandType.StoredProcedure;
_select.Connection = Connection.GetConnection;
SqlDataAdapter _daPerson = new SqlDataAdapter(_select);
DataSet _personDs = new DataSet();
_daCountry.Fill(_personDs, "[TBL_Person]");
return _personDs;
}
此方法将返回包含以下列的数据集:
Person_ID, FirstName, LastName, Age, Location
但我希望我的方法返回具有以下列的数据集:
FirstName, LastName, Age
如何操作?如果不想选择所有列,请相应地更改存储过程 如果要在C中进行更改,可以通过table.columns.Removename删除不需要的列:
如果您不想更改数据库中的SP,请更改.cs文件中的GetPerson方法,如下所示:
public DataSet GetPerson()
{
SqlCommand _select = new SqlCommand();
_select.CommandText = "SP-GetAllPerson";
_select.CommandType = System.Data.CommandType.StoredProcedure;
_select.Connection = Connection.GetConnection;
SqlDataAdapter _daPerson = new SqlDataAdapter(_select);
DataSet _personDs = new DataSet();
_daPerson.Fill(_personDs, "[TBL_Person]");
_personDs.Tables["TBL_Person"].Columns.Remove("Person_ID");
_personDs.Tables["TBL_Person"].Columns.Remove("Location");
return _personDs;
}
否则,您可以相应地更改存储过程。同时显示sp代码。。我如何将表的特定列添加到数据集中?相应地更改存储过程。如果您想在C中更改它,问题是如何从数据集的表中删除特定列?选择要在数据集中显示的列。哦,这取决于我的sp,如果我选择FirstName、LastName、Age;它可以工作吗?请记住,数据集本身不会有任何列,而是数据表
DataSet dsPerson = GetPerson(new[]{"FirstName", "LastName", "Age"});
public DataSet GetPerson()
{
SqlCommand _select = new SqlCommand();
_select.CommandText = "SP-GetAllPerson";
_select.CommandType = System.Data.CommandType.StoredProcedure;
_select.Connection = Connection.GetConnection;
SqlDataAdapter _daPerson = new SqlDataAdapter(_select);
DataSet _personDs = new DataSet();
_daPerson.Fill(_personDs, "[TBL_Person]");
_personDs.Tables["TBL_Person"].Columns.Remove("Person_ID");
_personDs.Tables["TBL_Person"].Columns.Remove("Location");
return _personDs;
}