« Home | Converting DataView into DataTable » | Deleting TFS Project »

Querying the DataTable in .Net

The best method for holding data collection in .net programming is by using DataTable.

By holding the data in datatable enables the programmer to query it, whenever needed and therefore avoiding connecting to the database.

Lets assume we have a datatable with some user registration data having UserName and State columns.

Now we are Querying the DataTable for a username like 'narenmcpd'.

####Code Starts####
string searchQuery = "USER_NAME LIKE ('%narenmcpd")";

DataView objdv = new DataView(objdt, searchQuery, "", DataViewRowState.CurrentRows);

####Code Ends####

Thats it, now we got the result into the dataview objdv.

If nothing meets the query, the dataview objdv will be null.

This is the way of querying the datatable in .net programming.

we can query n number of columns by manipulating the searchquery.

For example, if we need to query the datatable for both the UserName and State columns in the above example.

####Code Starts####

string searchQuery = "USER_NAME LIKE ('%narenmcpd")";

searchQuery = searchQuery + " AND state LIKE ('%delhi%')";

DataView objdv = new DataView(objdt, searchQuery, "", DataViewRowState.CurrentRows);

####Code Ends####

Happy Coding..

Labels: , , , ,

Post a Comment

Translate

About me

Narayanan
Senior Software Engineer
Microsoft Certified Professional Developer
MCPD

Chat

Visitors