I have a Silverlight application that contains a window that lists several values in multiple columns. The first column contains fields to be retrieved from a database, the second column contains table names.
Multiple fields can be selected from the first column and only one field can be selected from the second column. So, the idea is to build a query that can select multiple columns from one of several tables (assume the column names are the same for each table).
My question is how do I pass these values into a WCF Data Service method and return an untyped dataset back to the calling Silverlight application? I will have no way of knowing the columns to fetch or the table to use until run-time. That means I cannot define a class to be used to return the data back from the WCF data service to Silverlight.
Any ideas on how to accomplish this?
Thanks
Silverlight doesn't have a datatable or dataset construct. However, you can fake it with nested lists. This guy put together the silverlight datatable code, and also shows how it can be serialized and sent over WCF:
http://blogs.telerik.com/blogs/posts/10-01-22/how_to_serialize_your_datatable_to_silverlight_using_wcf_service.aspx
As for getting the query to the server, you have a few options. You can build linq expression trees dynamically (the same way that domaindatasource does under the covers) and use that to query right from the client. You could also send your search parameters in a serialized form over to the server and construct the query there. Again, you'll either have to build linq expression trees if you want to use LinqToEntities for the query, or you could go old school and just build up an SQL query. If you go SQL, make sure you protect against SQL injection.
I could also suggest that you vote for the dataset/datatable feature to be added to silverlight, which would make your solution a little easier to develop. You can vote for it here.
Related
I often use the DataTable class in my .NET WCF services, since many of our SPs require TVPs. As far as I know, DataTables are the only way of passing TVPs to SPs.
It just occurred to me that similarly to how tables, in which information is stored according to rows and columns are useful, that the DataTable class may be useful beyond just as a means of interfacing with SQL Server TVPs.
Actually... thinking about this, I have previously written code that iterated over a DataTable's rows, building up an HTML string. However the main reason we used a DataTable as because the same table could be passed to SQL Server as a TVP.
Looking at the docs: https://msdn.microsoft.com/en-us/library/system.data.datatable%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396, it looks like you can effectively create relational object models using DataTables.
Would using DataTables be an effective way of caching data retrieved from a SQL Server in a service?
Another potential use-case that comes to mind... Would there be any benefit of using a DataTable for a collection instead of List<MyType>?
Datatables are slower than Lists/Enumerables, and its better to use dataAdapter while reading data if you really care about performance.
But Datatables can be really useful as a item source for grids, where you want to just publish whole table data on the UI and no need to specify each column individually as in the case of List.
I'm building a small part for my ASP.NET MVC website the will require the following steps:
Query my SQL Server DB for data from a particular table.
For each data row returned take that data and run another query (stored procedure) that returns some values.
take those values and run a calculation.
now that I have that calculation I will need to take it and store it along with some other data items from the first query in-memory (or, not if you think otherwise) and filter and sort. after filtering and sorting displaying the results to the user.
What do you guys recommend doing for such a scenario where you need to have an in-memory data representation that will have to be manipulated? should I just stick with DataTable? I found a component called QueryADataSet which allows running querys against .NET DataSets and DataTables - does anyone knows it? how about using that kind of solution? recommended?
would love to hear your thoughts...
Thanks
Change the website to behave as follows:
Send a single query to SQL that uses set operations to apply the calculations to the relevant data and return the result.
This is not a joke nor irony. Using the app server for doing 'sort' and 'filter' is not the proper place. Aside from the lack of an adequate toolset, there are issues around consitency/caching behavior. Data manipulation belongs to the back end, this is why you use SQL and not a key-value store. Not even mentioning the counter-pattern of 'retrieve a set and then call DB for each row'.
If the processing cannot be performed on the database server, then LINQ is a good toolset to filter and sort data in the application.
I want to refactor a logic to filter the grid of my application. I'd like to implement this logic only in the Database.
So, I have a grid which displays the data from more tables from DB. There are some textBoxes and comboBoxes where it sets the data for each filter it wants.
Now, it is a HUGE Stored Procedure in Database which works this way:
Initially selects [ALL DATA] from tables into a temporary table ,
after that,
according to fields that were filled with data (from application filter), it removes from [ALL DATA] that info which is NOT LIKE SELECTED FILTER
And so on foreach parameter which is set in the filter.
This way consumes much time, because initially selects all data, and then slowly removes those which don't need.
I don't want to create SQL queries on client side. I'd like to do that only to Database, or .... iimmmm, i don't know...
Which would be a best way, very optimized, which would run fast and return results in short time as possible?
I use C# and .NET 4.0 for client side, and MSSQL DB.
Thank you for advices.
Amend your stored procedure so that it only selects the required data into the temprorary table in the first place, rather than selecting everything and then deleting what isn't required.
That's what ORMs are for. Don't build a dynamic SQL statement on the client side. Rather, build a LINQ query filter by filter. You'll get the benefits of being entirely dynamic, without the risk of SQL injection.
You can look at this for an example.
When I load from the database I use one store procedure which loads the DataItem and any Data associated with it. This comes back in one DataSet with two tables, the first table has one row and describes the DataItem and each row in the other table describing the related Data.
This DataSet is then used to populate my objects.
My problem comes when I have to save the objects back to the database. I am currently saving the DataItem and then looping through all of my Data and performing a save on each one. Completely horrible way to go about doing it, I know. It's both slow and it's not transactional.
So what I'd ideally like to do is convert my objects back into my DataSet and then save it all back to the database in one efficient transactional operation. What code do I need on the C# side to make this transactional and to allow me to pass back a DataSet. I presume this will involve using a TableAdapter. But given that I have two tables how will this work? What do I use on the SQL side - Can I use store procedures? (I would like to avoid having SQL in my C# project) Would I need to write something that will handle cycling through a datatable to save each record?
What's the best way to go about doing all this? This will form the lynchpin of a project I'm working on so I want it to be as fast and efficient as it can be!
(.NET 4.0 and SQL 2005)
Did not use TableAdapter in the end as it was more effort than it was worth.
From the comments:
http://msdn.microsoft.com/en-us/library/4esb49b4.aspx
i know this is an age old question, but this is my scenario
This is in C# 2.0
Have a windows application which has a datagridview control. This needs to be populates by making a webservice call.
I want to achive the same functionality on the data if i were to use a direct connection and if i were using datasets, namely like paging and applying filters to returned data. i know returning datasets is a bad idea and am looking to find a good solution.
I might look at ADO.NET Data Services, aka Astoria, in VS2008 SP1.
This allows you to expose data over a web-service (WCF exposing ATOM, IIRC) - but you don't need to know all these details: the tooling worries about that for you: you just get regular IQueryable<T> sources on a data-context (not quite the same as the LINQ-to-SQL data-context, but same concept).
The good thing here is that a LINQ query (such as filtering (Where), paging (Skip/Take) etc) can get composed all the way from the client, through the web-service, and down to the LINQ-enabled data store (LINQ-to-SQL or Entity Framework, etc). So only the right data comes over the wire: if you ask for the first 10 rows (of 20000) ordered by Name, then that is what you get: 10 rows out of the database; 10 rows over the wire, no messing.
Write a custom class (MyDataItem) that'll hold your data. Then you can pass a List<MyDataItem> or some collection of MyDataItem and bind to your grid.
Paging, filtering, etc. would need to be implemented by you.
There is no way to get the binding behavior you automatically get with a DataSet if you are going through a Web Services data layer. You would have to create your own proxy class that supports all the databinding functions and persists them through your web service calls. Depending on your application's environment, you may want to batch up modifications to avoid excess round trips to the web services.
fallen888 has it right - you will need to create a collection class of List or a DataTable, fill it with the output from the webservice data stream, and handle paging and filtering yourself yourself.