DataSet querying like SQL table

602 views Asked by At

Is there a way to query a DataSet like a SQL table? Eg. I want to check in DataSet if username matches password. I could use this code like this:

foreach (DataRow row in dataset.Tables[0].Rows)
{
   MessageBox.Show(row.ItemArray[1].ToString());
}

but I just want it to select row which have given username, not to iterate entire table.

Thanks.

2

There are 2 answers

0
podiluska On BEST ANSWER

You can use

 dataset.Tables[0].Select(....)

http://msdn.microsoft.com/en-us/library/det4aw50.aspx

But you shouldn't. In almost every circumstance, the SQL server will be quicker at finding the match.

Also, you should hash your passwords

0
A. Agius On

You can use the RowFilter

dataset.Tables[0].DefaultView.RowFilter = "UserName='MyUserName' 
And Password='0x0000000000000000'"

if(dataset.Tables[0].DefaultView.Count > 0){
//User Found
}else{
//User Not Found
}

More information below;

http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview.aspx

That said, ideally this filtering is done in SQL and not .NET Code.