How to access an column with special characters using DataTable.Select()?

我与影子孤独终老i 提交于 2019-12-20 07:12:55

问题


I have a DataTable with column such as # of Students and would like to sort by this in descending order. Here is my code:

...
dt.Columns.Add(new DataColumn("# of Students", typeof(string)));

// do some stuff... add records etc.

// A runtime error occurs here: "Cannot find column '# of Students'"
var rows = dt.Select("","'# of Students' desc");

// this is just fine.
rows = dt.Select("","# of Students");

How can I access this column if has special characters in its name?


回答1:


You should use [] brackets, like this :

var rows = dt.Select("","[# of Students] desc");



回答2:


You can use both [] or `` syntax. Both following snippets are correct:

var rows = dt.Select("","`# of Students` desc");

var rows = dt.Select("","[# of Students] desc");


来源:https://stackoverflow.com/questions/10081523/how-to-access-an-column-with-special-characters-using-datatable-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!