Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Task 4 – Querying a DataSet

1. Return to the CreateCustomer method and update it to read data in from the Northwind DataSet created by the designer (notice the return type also has to change):

static NorthwindDS.CustomersDataTable CreateCustomers()

{

SqlDataAdapter adapter = new SqlDataAdapter(

"select * from customers",

ConfigurationManager.ConnectionStrings

["LINQOverview.Properties.Settings.NORTHWNDConnectionString"]

.ConnectionString);

 

NorthwindDS.CustomersDataTable table =

new NorthwindDS.CustomersDataTable();

 

adapter.Fill(table);

 

return table;

}

 

2. We need to replace the connection string key in the code above (“LINQOverview.Properties.Settings.NORTHWNDConnectionString”), with the actual key used in the app.config file. In Solution Explorer, double-click the app.config file.

3. Under the <connectionStrings> node, you will see an <add> node. Within this node, the connection string key is the value in quotes after name=. Copy this quoted value.

4. Return to Program.cs and paste the key you’ve copied in place of “LINQOverview.Properties.Settings.NORTHWNDConnectionString” in the body of the CreateCustomers method.

5. Return to the ObjectQuery method. You will need to make one update to the print statement (and update Main to call ObjectQuery again):

 

static void ObjectQuery()

{

var results = from c in CreateCustomers()

where c.City == "London"

select c;

foreach (var c in results)

Console.WriteLine("{0}\t{1}", c.CustomerID, c.City);

}

 

static void Main(string[] args)

{

ObjectQuery();

}

 

Move the mouse over c to notice that the objects returned from the query and iterated over in the foreach loop are no longer Customers, rather they are CustomerRows. These objects are defined in the DataSet and are a strongly typed view of the Customer Table in the Northwind Database.

 

6. Press Ctrl+F5 to build and run the application. Notice the output still only contains those customers that are located in London. Press any key to terminate the application.

 

 


Date: 2015-02-03; view: 694


<== previous page | next page ==>
Task 4 – Transforming XML Output | Task 2 – Creating Object Mapping – Creating an Object and Providing Attributes
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)