How to execute SQL Query inside DataSet?
-
I want to execute SQL Query within a Dataset. I don't know how it should be done. I just have some sample Query & sample data.
'test.xml file Contains 3 columns:
'ID, Name, Location
'Sample Values:
''ID, Name, Location
''1, abc, loca
''2, ijk, locb
''3, xyz, locc
''4, uvw, locd
Dim ds As New DataSet
ds.ReadXml("test.xml")'Sql Query is: "Select Name fom ??? where ID=1" 'Is this possible to to retrive the output of that Sql query from test.xml using DataSet? 'If DataSet does not support this type of operation then how can I execute that or any other query?
Here Is the test.xml File:
<?xml version="1.0" standalone="yes"?>
<DataSetSchema>
<Tab1>
<ID>1</ID>
<Name>abc</Name>
<Location>loca</Location>
</Tab1>
<Tab1>
<ID>2</ID>
<Name>ijk</Name>
<Location>locb</Location>
</Tab1>
<Tab1>
<ID>3</ID>
<Name>xyz</Name>
<Location>locc</Location>
</Tab1>
<Tab1>
<ID>4</ID>
<Name>uvw</Name>
<Location>locd</Location>
</Tab1>
</DataSetSchema> -
I want to execute SQL Query within a Dataset. I don't know how it should be done. I just have some sample Query & sample data.
'test.xml file Contains 3 columns:
'ID, Name, Location
'Sample Values:
''ID, Name, Location
''1, abc, loca
''2, ijk, locb
''3, xyz, locc
''4, uvw, locd
Dim ds As New DataSet
ds.ReadXml("test.xml")'Sql Query is: "Select Name fom ??? where ID=1" 'Is this possible to to retrive the output of that Sql query from test.xml using DataSet? 'If DataSet does not support this type of operation then how can I execute that or any other query?
Here Is the test.xml File:
<?xml version="1.0" standalone="yes"?>
<DataSetSchema>
<Tab1>
<ID>1</ID>
<Name>abc</Name>
<Location>loca</Location>
</Tab1>
<Tab1>
<ID>2</ID>
<Name>ijk</Name>
<Location>locb</Location>
</Tab1>
<Tab1>
<ID>3</ID>
<Name>xyz</Name>
<Location>locc</Location>
</Tab1>
<Tab1>
<ID>4</ID>
<Name>uvw</Name>
<Location>locd</Location>
</Tab1>
</DataSetSchema>A dataset returns a collection of DataTables, in this case it would only contain one Table. I don't think it is possible to query DataSets using SQL. You would need to iterate through the table until you found an ID matching 1 and retrieve the name. Personally I prefer to use XML literals and LinQ to retrieve data from xml files like this
Dim datatest As XDocument = XDocument.Load("Test.xml")
For Each student In From element In datatest...<Tab1>
If student...<ID>.Value = 1 Then
name = student...<Name>.Value
Exit For
End If
Next
MsgBox(Name)That makes everything very concise and readable. Hope this helps. Happy Coding
-
A dataset returns a collection of DataTables, in this case it would only contain one Table. I don't think it is possible to query DataSets using SQL. You would need to iterate through the table until you found an ID matching 1 and retrieve the name. Personally I prefer to use XML literals and LinQ to retrieve data from xml files like this
Dim datatest As XDocument = XDocument.Load("Test.xml")
For Each student In From element In datatest...<Tab1>
If student...<ID>.Value = 1 Then
name = student...<Name>.Value
Exit For
End If
Next
MsgBox(Name)That makes everything very concise and readable. Hope this helps. Happy Coding
Is that only one way to do quering a dataset? I tried
Dataset.Select("ID=1")
but it is not enought for my requirement. anyway thanks. -
Is that only one way to do quering a dataset? I tried
Dataset.Select("ID=1")
but it is not enought for my requirement. anyway thanks.You could always bind the data to a bunch of controls e.g. bindingsource, bindingnavigator and datagridview and then use the FILTER property of the BindingSource, as it acts just like a 'where' clause in SQL.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.commodified on Thursday, July 15, 2010 10:10 AM