如何选择从DataSet到DataTable中的哪些列? [英] How can I select what columns come in from a DataSet into a DataTable?

查看:188
本文介绍了如何选择从DataSet到DataTable中的哪些列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为处理数据的新手,我希望我正确地问。如何选择从DataSet到DataTable中的哪些列?我知道我可以使用...填充DataTable。

Being new to working with Data, I hope I'm asking this properly. How can I select what columns come in from a DataSet into a DataTable? I know I can fill a DataTable by using...

DataTable table = dataSet1.Tables[0];

但这会带来所有的列。

but this brings in all the columns. How can I fill a DataTable with only certain columns?

我使用的是.NET 3.5,C#和一个SQL CE 3.5单表数据库。

I'm using .NET 3.5, C#, and a SQL CE 3.5 single table database.

感谢。

推荐答案

    // Assumes that connection is a valid SqlConnection object.

string queryString = "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");

DataTable table = customers.Tables[0];

您可以选择要选择的列,而不是CustomerID,CompanyName。

Instead of "CustomerID, CompanyName" you can put the columns you want to select.

要进一步学习,请 MSDN 链接。

For further learning check this MSDN link.

这篇关于如何选择从DataSet到DataTable中的哪些列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆