Call stored procedure in loop in wcf - c#

I have a problem with calling WCF service from ajax.
I pass to WCF an array of objects, and for every object, I need a stored procedure to be called via ADO.NET.
The problem is when I call this procedure in the loop, the application starts to lag.
Can you tell me, how is possible to fix it ?

Is it a stored procedure you can modify? You might get significant performance benefits by modifying the stored procedure to use table-valued parameters. That way instead of calling the stored procedure once for every object, you're calling the stored procedure once and passing all of the objects.
The linked documentation is thorough, although you might want to google for other examples. The first time through using them you have to learn some new steps, but once you get through that it's a powerful tool for scenarios like this.
On the SQL server you create a new table-valued type. The syntax is very similar to defining a table. Then you modify your stored procedure to receive that parameter. Within the procedure you select from the parameter just like you do from a table.
In your application you create a DataTable which corresponds to the table you've defined and then populate it with the rows of data you want to send.
If you've ever seen anyone doing weird stuff to pass multiple values to a stored procedure (like using comma-delimited strings) this is the antidote.

Related

How to pass complete model to stored procedure in c#

Is there any way to pass a complete model to stored procedure using C#?
We can pass parameters to stored procedure but I want to send complete model.
See me answer to other question for complete code sample: SqlBulkCopy Ignore Duplicate Records of Datatable From DataBase
Basically, you can pass a whole datatable. The Merge part is likely not relavent to you.
If you are using SQL Server 2008 or above you can use Table Valued Parameters.

Does passing a Table to a Stored Procedure incur multiple database calls?

I have a large set of data. Now i am passing that data in the form of concatenated string with delimiters. In the stored procedure i am parsing the string and storing responses to table. Is this is the best approach OR passing Table to stored procedure is best approach.
I heard that passing table to stored procedure will incur multiple database calls for each row. Is this is true?
You mean table-valued parameters? No, it won't cause "multiple database calls for each row", it's just another way of passing a large chunk of data into a procedure.

Full Data Binding to a stored procedure?

Is it possible to do data binding to a stored procedure, similar to a table or view ?
(i.e. including select, update, delete, insert)
for selecting, I'm currently executing the procedure with ExecuteReader(), read it into a DataTable and then bind the table to a grid. But now, how to write back changes from the datatable into the database? Is there a "simple", built-in method in the .NET framework?
No, it isn't possible. A stored procedure could produce the returned results in any way possible with T-SQL code and it is not possible to create an update by calling the same procedure as was used to read the data.
A more modern way to work with data access is to use an OR-Mapper such as entity framework. Depending on your type of application (web applications where the data is sent to the client and then posted back and interpreted as new objects are typically a bit harder) it might be possible to do a simple data binding and save the changes back to the DB.
It is not possible with Stored procedure, but something similar can be achieved with SqlCommandBuilder
Here is a full sample which describes how to do that http://support.microsoft.com/kb/307587

Dynamic SQL based stored procedure call in Entity Framework

For one of our recent projects, we created a stored procedure which generated SQL and executed it in the end. The purpose of the stored procedure was to create pivots based on dynamic columns.
When trying to access it using Entity Framework using the usual function import when I tried to access the stored procedure, it would return anything as it requires a dynamic type to store the retrieved data.
Which in our case was a dynamic query and linq was unable to get the returned columns. So to work around what I did was call the stored procedure in the traditional way i.e. creating a DataAdapter and SqlCommand object and SqlConnection object.
But what is the proper way of calling this kind of stored procedure using Entity Framework?
Thanks in advance.
Entity framework doesn't support dynamic result sets from stored procedures. It also doesn't support stored procedures using dynamic SQL because it cannot get static result set declaration from the procedure. So you must either ensure that your procedure will always return static type (same number of columns with same names) or you must use traditional ADO.NET to execute that procedure.
Following steps can be followed:
Store the dynamic part of SP inside a variable and the print that variable at end of the SP.
execute the SP and execute it with some data.
open the Messages tab in Result window.
copy the code that is written after (x row(s) affected);
paste that code in the SP and comment out everything else until variables declaration.
execute the new modified SP and add it to the entity framework. This time, entity framework will make a complex type which you want.
uncomment the previous commented code and delete the data that you copied from Messages tab and execute it again.
Follow the same process every time you add or remove columns from the SP.

Is there a way to pass C# objects to SQL stored procedure

Some background to the problem:
I have a hierarchy of objects that looks like this:
Each 'CallAction' will have Queue OR a Question based on the 'ActionType'.
When a workflow is created through the website it is limited to 2 levels of questions. All of this has been implemented in C# and the tables created in the SQL Server. I now need to find a way to add this all to the database.
As I am extending Website Panel (an open source project) I'm trying to follow the same standards they have been using this means every call to the database is done using stored procedures.
My question is: is there any way I can just pass a 'workflow' to the SQL stored procedure and handle the multiple different levels in SQL, or am I going to have to create multiple stored procedures and handle the different levels in my C# code calling different stored procedures based on the hierarchy?
I also understand that I could access the database directly using SQLCommand or LinQ but I would rather use stored procedures if that is possible.
No such thing as objects in SQL Server. You could pass properties as named parameters to a stored proc (this way no need to maintain their order), and have the stored proc put them where they need to go: update, insert, delete.
For entire workflows, you should use matrix parameters. Google "directed network matrix representation". A single matrix will then represent all of your objects and their directed links to each other (1 - forward, -1 - back). If you want to get a little fancier, instead of using numbers, you can create a string matric which stores object names along with direction. If you multiply direction by a Real number, you get connection weight (e.g. probabilities).
But ALL this is a very backwards way of doing it. You should create this kind of adaptors in C#, and do bulk inserts/updates.
Sorry I couldn't be more helpful. Good luck.
EDIT: since your "workflow" looks a lot more like an ontology than a real workflow, you can see about SPARQL and such - ontology-specific SQL languages. Or Google "storing ontologies in SQL databases"
If you can express the hierarchy in XML, then you could pass it as an XML data type to your stored proc. The proc could then read the XML using XPath etc.
I think CLR Stored Procedures can help you:
http://msdn.microsoft.com/en-us/library/ms131094.aspx

Categories

Resources