4

I have a DataSet named dsView that contains data. Here is the code i use to read the XML:

dsView = new DataSet();
dsView.ReadXml(@"\c:MyXml.xml");

The data has a table named MEN with fields Fname and Lname. How do I run a query on this dsView? (for example: select * from MEN where Fname = 'zz')

Linger
  • 14,942
  • 23
  • 52
  • 79
Gali
  • 14,511
  • 28
  • 80
  • 105

6 Answers6

16

You cannot run complete SQL statements on a DataSet - but the DataTable inside your DataSet does have a method called Select(string) which selects an array of DataRow object that match a certain criteria.

Check out the MSDN docs on DataTable

You would probably have to do something like (untested):

DataTable tblMEN = dsView.Tables["MEN"];
DataRow[] results = tblMen.Select("Fname = 'zz'");
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

I don't think you can run a SQL query on a DataSet, since it doesn't have a query engine. But you could try LINQ to DataSet.

Also, if you are only interested in the data (and not the databinding properties of the DataSet), I suggest you use LINQ to XML to query the document.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
0

Here is an example that I used to make a menu.

Dim dstpage As DataSet
dstpage = businessService.GetData()
Dim userTable As DataTable
userTable = dstpage.Tables(0)
If userTable.Rows.Count > 0 Then
   Dim results As DataRow()
   results = userTable.Select("PAGE_GROUP_NAME='People'")
   For i As Integer = 0 To results.Count - 1
       FilePath = userTable.Rows(i)("FILE_PATH").ToString()
       PageName = userTable.Rows(i)("PAGE_NAME").ToString()
   Next
End If
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
0
// Create a table of five different people.
    // ... Store their size and sex.
    DataTable table = new DataTable("Players");
    table.Columns.Add(new DataColumn("Size", typeof(int)));
    table.Columns.Add(new DataColumn("Sex", typeof(char)));

    table.Rows.Add(100, 'f');
    table.Rows.Add(235, 'f');
    table.Rows.Add(250, 'm');
    table.Rows.Add(310, 'm');
    table.Rows.Add(150, 'm');

    // Search for people above a certain size.
    // ... Require certain sex.
    DataRow[] result = table.Select("Size >= 230 AND Sex = 'm'");
    foreach (DataRow row in result)
    {
        Console.WriteLine("{0}, {1}", row[0], row[1]);
    }

you can use this code.i hope it's help you.

Manish Singh
  • 934
  • 1
  • 12
  • 27
0

AFAIK You can only run a Sql Like query on a DataTable with the Select method.

You would use this overload of DataTable.Select here to do something like this:

DataRow[] foundRows = dsView.Table[0].Select("Fname = `zz`");

dsView.Table[0] should point to the table of MEN and should have a column Fname

The expressions valid are the same as DataColumn expressions:
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

gideon
  • 19,329
  • 11
  • 72
  • 113
0
dsView.Table[0].DefaultView.RowFilter = "Fname = 'zz'";
// now default view contains the filtered rows
//ds.Table[0].DefaultView

More about DefaultVeiw on msdn

Waqas Raja
  • 10,802
  • 4
  • 33
  • 38