I have a stored procedure that is a class in my project. I gain the data returned by following:
EMTS.SPs.PrcEMTSDataLookup(so).GetDataSet().Tables[0].Copy()
This is placed in a dataset. The issue is the stored procedure has several selects and each select needs to be placed in a separate grid.
I have tried IDataReader and IDataAdapter but have not had any luck pulling data from the stored procedure object. I have research microsoft for information on IDataAdapter and the example they give uses SqlDataAdapter which is not necessary for me since the stored procedure is an object in the project.
So how do I get the different result sets in the sp and put them in separate grids?
Related
Good Afternoon,
We are currently working on a tool, which retrieves all the Stored Procedures in a given database, which then extracts each Table being used in each Stored Procedure.
Currently, I got all of the Stored Procedures in a List, don't know if there are any parsers available that I can pass the Stored Procedure and returns the Tables being used.
The plan is then to link each Stored Procedure with the Tables being used in a Graph Database.
Thanks
I think there is no prewrited methods to get that. You have to request the database tables, then check in each stored procedure query if it contains the table name.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
This query will return the tables in the database
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.
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.