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.
Related
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.
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
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.
For a while i have been googling how to use LINQ and connecting my stored procedures in c#. Anyone out there can give me some form of hint or some sort of help? That would be greatly appreciated.
You can have the Visual Studio / DBML designer scaffold the stored procedure for you and generate the required code to call it, with strongly typed parameters. Just drag the required stored procedure within the DBML designer:
dragging it within an existing entity will make your stored procedure returns a collection of instances of that type, if possible;
dragging it to the Stored procedures panel will make it returns a collection of rows specific to that stored procedure.
Easiest way is to use a Linq-to-Sql layer or .dbml. Scott Guthrie has a great series of blog posts on setting this up. Start here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
Please guide me how to save Master-detail record in sql server database. All of my data is in a single form.
I am not getting how to pass detail records into stored procedure ? Is there any way other than Transation+ bulk insert ? It looks like complex.
thanks
This might help: Passing a DataSet to a SQLCLR Stored Procedure