我在我的数据访问类下面的代码:如何显示DataTable返回的结果?
public IEnumerable<Object> getAllPersons()
{
string CommandText = "Select * from Person";
SqlCommand oCommand = new SqlCommand(CommandText, oConnection);
oCommand.CommandType = CommandType.Text;
// Create a datatable and SqlDataAdapter
SqlDataAdapter oAdapter = new SqlDataAdapter();
oAdapter.SelectCommand = oCommand;
DataTable oDataTable = new DataTable();
try
{
// Open Connection and fill the datatable using the SqlDataAdapter
oConnection.Open();
oAdapter.Fill(oDataTable);
return oDataTable.AsEnumerable();
}
catch (Exception oException)
{
throw oException;
}
finally
{
// Close the SqlConnection
oConnection.Close();
}
}
在我的表现层我需要绑定这个返回的DataTable到我的网格,这是我的代码:
DataAccess mydataaccess = new DataAccess();
IEnumerable<object> personList=mydataaccess .getAllPersons();
GridView1.DataSource = personList;
GridView1.DataBind();
这将导致以下:
如何显示实际列和数据起因于大taTable?
你是如何设法编译这段代码的? – Steve 2014-11-22 14:50:42
你是什么意思? – user3340627 2014-11-22 14:52:06
@Steve,我想我明白你的意思,我的代码中有一个缺失的部分,请检查编辑后的代码 – user3340627 2014-11-22 14:53:05