将多个参数传递给存储过程

时间:2016-07-26 13:09:31

标签: sql-server ado.net linq-to-entities

我触发存储过程并将参数从Visual Studio传递到SQL Server存储过程。

这是代码:

var cId = new SqlParameter("@clientId", clientId);

var result = _context.Database.SqlQuery<DamageEventsDTL>("SPDamageEventsDTL @clientId", cId );

但我需要传递多个参数(intDateTime和整数列表。)

我是怎么做到的:

 int clientId = 5;
 DateTime date = new DateTime("2016-07-01");
 List<int> list= new List<int>(new int[] { 2, 3, 5 });

 var cId = new SqlParameter("@clientId", clientId);
 var dateEvents = new SqlParameter("@date", date);
 var freqEvents = new SqlParameter("@list ", list );

 var result = _context.Database.SqlQuery<DamageEventsDTL>("SPDamageEventsDTL @cId, @date, @list ", cId, dateEvents, list);

但这似乎是错误的方式。

知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

您还可以使用XML将参数发送到SQL,如下所示:

USE tempdb
GO
 DECLARE @Element XML;
          SET @Element='
<SavedOrderElement>
  <Data>
    <Age>5</Age>   
    <Nit>46546546546</Nit>
    <Name>Jeff</Name>   
    <LastName>Thalliens</LastName>
    <Email>VerizonBoughtYahoo@yahoo.com</Email> 
    <PokemonGo>Nothing</PokemonGo>
  </Data>  
</SavedOrderElement>' ;


;WITH ElementSavedOrders
AS
(       
    SELECT 
    CAST(c.query('data(Age)') AS VARCHAR(50)) as Age
    ,CAST(c.query('data(Nit)') AS VARCHAR(50)) as Nit
    ,CAST(c.query('data(Name)') AS VARCHAR(50)) as Name
    ,CAST(c.query('data(LastName)') AS VARCHAR(50)) as LastName
    ,CAST(c.query('data(Email)') AS VARCHAR(100)) as Email  
    ,CAST(c.query('data(PokemonGo)') AS VARCHAR(100)) as PokemonGo  
    FROM @Element.nodes('SavedOrderElement/*') as T(c)                  
)

SELECT * FROM ElementSavedOrders