Full Data Binding to a stored procedure? - c#

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

Related

data set in visual studio

i am creating an application using visual studio that use a database of course. i don't get why we create a data set as i tried some query without creating a data set and it worked perfectly. the queries i will be using are update, delete, insert and select (simple and complex ones).
so should i use the data set and why?
Note the database is a big one, and as i understood creating a data set will create a copy of the database, so will this make a storage (memory) problem?
You are looking for ways without a Dataset..
For INSERT, UPDATE and DELETE can use System.Data.Sql and System.Data.SqlClient, open your own SqlConnection and proceed https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=dotnet-plat-ext-3.1 .... but for reads (SELECT), it is practical to use a DataSet. On this level (below Entity Framework !) the DataSet has a Fill() method, you can fill it with any data you want. The only class I know of that can read without a DataSet is DataReader, refer https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader
NOTE: as mensioned in the comments above, these low level methods to access a database are inherently unsafe, because you have to specify and pass explicit SQL to ADO, making user input vulnerable.. This can be avoided using a parametrized DataAdaper+Dataset, or Entity Framework, in either way you can avoid SQL injection.
If you have a big database you should consider using an ORM like Entity Framework. By using the old ADO .Net a dataset would help you handling your data. Inside it you will have a DataTable that represents your data and so, whenever you make a select the rows of your database will be stored temporarily in your dataset/datatable. That said, you don't need all your database there, but only the rows you need to work on.
Every time you add a new record to the datatable, it is inserted flagged as new. When you modify a row, it is flagged as modified or deleted, when it is the case. So after all handling you can save your dataset back to the database.

Call stored procedure in loop in wcf

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.

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.

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.

Linq-To-SQL CreateDatabase doesn't create stored procedures

to-SQL DataContext and I run:
datacontext.CreateDatabase()
This works fine.
Recently I dragged a stored procedure onto the methods pane. I was thinking this stored procedure was now part of the datacontext and would get regenerated when creating the database.
It doesn't seem to. Does anyone know why or how to make it happen?
The DataContext.CreateDatabase method creates a replica of the database only to the extent of the information encoded in the object model. Mapping files and attributes from your object model might not encode everything about the structure of an existing database. Mapping information does not represent the contents of user-defined functions, stored procedures, triggers, or check constraints. This behavior is sufficient for a variety of databases.
SPs not part of that http://msdn.microsoft.com/en-us/library/bb399420.aspx
To my knowledge stored procedures must be declared in the Sql Server Management Studio (or such tool) and can'e be done via LINQ.

Categories

Resources