Procedure or function has too many arguments specified, C# & SQL Server - c#

I am developing my very first stored procedure in SQL Server 2012 and need advice concerning the error message I get.
C# code:
Stored procedure code:

In the example you showed, you are not using the stored procedure. The whole Program.sqlcmd.CommandType isn't being used, as you specify a different SQL string (st) to use.
You need to look at examples of how to do stored procedures from c#.
How to execute a stored procedure within C# program

Related

How to save a procedure against a database in SQL Server?

So i'm following a tutorial on how to connect a database to WFA and at this step he saves the procedure, but don't know where because he skips.
My question: How do I save it inside the folder because whenever I want to save, it wants me to choose a path and doesn't save directly in the "Stored Procedures" folder.
To answer your question SQL SERVER automatically saves your stored procedures within the Programmability -> Stored Procedures -> Your SP upon creation.
Note : Always remember to refresh the Stored Procedures Folder after you have created a new Stored Procedure, otherwise you might not be able to see it (but its there, dont worry)
UPDATE
Here is a TSQL statement to create a Stored Procedure (of course you can change it to your own vision) :
USE YourDatabaseName
GO
CREATE PROCEDURE SchemaName.SPName
AS
SELECT * FROM SchemaName.TableName
GO
This will Create a Simple SP for your service (assuming you have excuted it). Now can look for it as above.
Remember You need to look for it within your SQL Server Management Studio. not within Visual Studio (Which you will be able to Call the SP later within your C# code using An ORM)
Suppose you are creating a procedure like below
CREATE PROC Test
AS
SELECT * FROM table_name
If you write this in SQL Server Management Studio and Execute (F5) against a DB, this will come under Programmability -> Stored Procedures.
Just execute the code and refresh the DB in Object Explorer and check under
Programmability -> Stored Procedures
OR
If you are trying to create the procedure from C# code, Just execute it like normal sql query through the C# code against the DB.
This will also create a procedure under Programmability -> Stored Procedures. Check SSMS in the same way as above to see.
OR
If you are trying to execute a stored procedure through C# code refer this

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.

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.

stored procedure in SQL CLR

How do you write a stored procedure using C# in SQLCLR?
Currently I am using SQL Server Management Studio and I write stored procedures using T-SQL such as create proc sp_item as .....
See:
Building my first SQL CLR stored procedure
CLR Assembly RegEx Functions for SQL Server by Example
Choosing between CLR and T-SQL stored procedures: a simple benchmark
Basically, there are Visual Studio templates which allow you to get started with SQL CLR projects. Fill in the blanks, write your actual code, and you can even deploy those CLR assemblies into SQL Server directly from within Visual Studio.
One word of caution: I would refrain from doing set-based updates and inserts and stuff like that in a SQL CLR stored proc - for that, T-SQL stored procs are just plain better and faster.
SQL-CLR is great to extend SQL Server with stuff like string manipulation, date handling, ability to call e.g. web services or other stuff. I would recommend against trying to replace all T-SQL stored procs with C# SQL CLR stored procs just because you can or just because it's cool - use the right tool for the right job! Mass operations on sets are better left to T-SQL.
In addition to the links provided by Marc, I also wrote a short tutorial on writing SQLCLR Table-Valued Functions (FYI, free registration is required to view articles on SQL Server Central):
CLR Table-Valued Function Example with Full Streaming (STVF / TVF)
I have also been writing a series of articles about working with SQLCLR in general:
Stairway to SQLCLR
Also, you should not use sp_ as a prefix in stored procedure names. That is a special syntax that degrades performance due to causing SQL Server to first check for that stored procedure in master, and then, if not found there, it will search the current database.

Passing a dataset to a SQL Server stored procedure

Here I am facing a problem that I want to pass a dataset to a SQL Server stored procedure and I don't have any idea about it and there is no alternate solution (I think so ) to do that, let me tell what I want ...
I have an Excel file to be read , I read it successfully and all data form this excel work book import to a dataset. Now this data needs to be inserted into two different tables and there is too many rows in Excel workbook so it is not good if I run it from code behind that's why I want to pass this dataset to stored procedure and than ........
please suggest me some solution .
Not knowing what database version you're working with, here are a few hints:
if you need to read the Excel file regularly, and split it up into two or more tables, maybe you need to use something like SQL Server Integration Services for this. With SSIS, you should be able to achieve this quite easily
you could load the Excel file into a temporary staging table, and then read the data from that staging table inside your stored procedure. This works, but it gets a bit messy when there's a chance that multiple concurrent calls need to be handled
if you're using SQL Server 2008 and up, you should look at table-valued parameters - you basically load the Excel file into a .NET DataSet and pass that to the stored proc as a special parameter. Works great, but wasn't available in SQL Server before the 2008 release
since you're using SQL Server 2005 and table-valued parameters aren't available, you might want to look at Erland Sommarskog's excellent article Arrays and Lists in SQL SErver 2005 - depending on how big your data set is, one of his approaches might work for you (e.g. passing as XML which you parse/shred inside the stored proc)

Categories

Resources