开发者

Whats the difference between using assigned variables and straight access

Considering below what are benefits, uses or gain of each and why do you think so? other than short way to access the data. btw is there a way to use Dat开发者_如何学编程aView in using (Resource){//Code} format.

DataTable dtUsers = UsersRepository.GetAllUsers();
Users.Name = dtUsers.Rows[0]["Name"].ToString();
Users.Address = dtUsers.Rows[0]["Address"].ToString();
...
...
...

and

DataView dvUsers = UsersRepository.GetAllUsers().DefaultView;

Users.Name = dvUsers[0]["Name"].ToString();
Users.Address = dvUsers[0]["Address"].ToString();
...
...
...


A DataView is there to be able to perform sorting, filtering, searching, editing, and navigation (msdn) without altering the data in the datatable itself. One of the biggest functionality thereof is for databinding to grid like controls so that you can control what data is displayed and how (Other than filtering/sorting it on sql level).

Another thing to note is that you can have multiple data views from one datatable, i.e. the one grid will display all the rows where the it's a male and the other grid will display all the females for instance.

Why would you want to use a using statement on a dataview? The whole reasoning behind a using statement is to dispose and close the object and release all resources after the code block is complete. That said, the only requirement to be able to use it in a using statement is it must implement IDisposable, which DataView does.

EDIT In your code example though, there should be no difference except for a slight performance gain when you don't use DataView because it creates an index, so essentially using the dataview is an unneccessary extra step if you aren't going to use it's functionality

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜