简体   繁体   中英

Call a stored procedure from .net

I have a stored procedure like this:

CREATE PROCEDURE up_action
(@id int
,@group varchar(30)=''
,@nom varchar(30)=''
,@compte varchar(30)=NULL
)
 AS
BEGIN
DECLARE @chrono int
......
select @date=date from users where compte=@compte
INSERT INTO dialog
(numappel,auteur,commentaire,etape,etapews,operant)
VALUES
(@numappel,@nomprenom,@dialogue,14,14,@nomoperateur)
SET @chrono = SCOPE_IDENTITY()      
select 'chrono'=@chrono
END

I want to call this stored procedure from an application written in C# with the possibility to give the parameters separated by, (actually their values) and after that to receive eventually a record set rs from which I can get the value of the variable like rs("chrono").

I know the possibility with creating every parameter and telling the type, the name, the value etc. But I want a method like in ASP because I have procedures with 100 parameters...

Normally, with POADO (Plain Old ADO.Net) you would do something like this:

using (SqlConnection conn = new SqlConnection(myConnectionString))
using (SqlCommand cmd = conn.CreateCommand())
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "up_action"
    cmd.Parameters.AddWithValue("@group", group);
    cmd.Parameters.AddWithValue("@nom", nom);
    cmd.Parameters.AddWithValue("@compte", compte);
    conn.Open();
    using (SqlDataReader rd = cmd.ExecuteReader())
    {
        if (rd.Read())
        {
            chrono = rs["chrono"];
        }
    }
}

But as Richard pointed out: you indeed better look into something like LINQ to SQL, as this wil generate a method for each stored procedure (which automatically has an argument per parameter of your stored procedure). See this link for more information.

Erm if you want to make the task easier look into something like LINQ to SQL.

So..the solution is:

SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = strQuery;
rdr = sqlCommand.ExecuteReader();
rdr.Read();
int chrono = Convert.ToInt32(rdr["chrono"])

And for 2 parameters

rdr.NextResult();
rdr.Read();
int str = Convert.ToInt32(rdr["str"]);

Thank you all for your answers

You may also want to look at the data access application block in Enterprise Libray as this provides good patterns to follow for data access. I'd also suggest you look at your SPs again and see if you can refactor them, 100 parameters sounds pretty excessive.

I imagine this is an insert/update procedure... 100 parameters sounds like too much to me, but if this is what you need, then you might want to look at table valued parameters (SQL 2008 only):

http://msdn.microsoft.com/en-us/library/bb675163.aspx

(First off though, I'd go with the above advice and have a look at an ORM like LINQ - this'll take the heavy lifting out of the problem)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM