在MVC中获取值多输出参数(存储过程)

时间:2016-06-24 03:42:07

标签: asp.net asp.net-mvc entity-framework asp.net-mvc-3 stored-procedures

我的存储过程:

html

当我在操作中调用此存储过程时,如何获取2个输出参数<!doctype html> <html> <head> <title>Line Chart</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.bundle.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <style> canvas{ -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; } </style> </head> <body> <div style="width:90%;"> <canvas id="canvas"></canvas> </div> <br> <br> <script> var randomColorFactor = function() { return Math.round(Math.random() * 255); }; var randomColor = function(opacity) { return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')'; }; var config = { type: 'line', data: { labels: [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3], datasets: [{ label: "My First dataset", data: [12,13,14,15,21,16,15,25,16,12,15,23,16,12,13,14,15,21,16,15,25,16,12,15,23,16,12,13,14,15,21,16,15,25,16,12,15,23,16,12,13,14,15,21,16,15,25,16,12,15,23,16], fill: false, borderDash: [1, 1], }] }, options: { responsive: true, title:{ display:true, text:'Chart.js Line Chart' }, tooltips: { mode: 'label', }, hover: { mode: 'dataset' }, scales: { xAxes: [{ display: true, scaleLabel: { show: true, labelString: 'Month' } }], yAxes: [{ display: true, scaleLabel: { show: true, labelString: 'Value' }, ticks: { suggestedMin: -10, suggestedMax: 25, } }] } } }; $.each(config.data.datasets, function(i, dataset) { }); window.onload = function() { var ctx = document.getElementById("canvas").getContext("2d"); window.myLine = new Chart(ctx, config); }; </script> CREATE PROCEDURE [dbo].[GetProduct] @ProdId int output @ProdCode nvarchar(20) output AS BEGIN SET @ProdId = 1 SET @ProdCode = 'PROD00001' END 的值?

2 个答案:

答案 0 :(得分:0)

create PROCEDURE [dbo].[GetProduct]
@ProdId int output
@ProdCode nvarchar(20) output
as
begin
    set @ProdId=1
    set @ProdCode='PROD00001'
    select @ProdId,@ProdCode
end

答案 1 :(得分:0)

将选择命令放在sp中

CREATE PROCEDURE [dbo].[GetProduct]
@ProdId int output
@ProdCode nvarchar(20) output
as
begin
    set @ProdId=1
    set @ProdCode='PROD00001'
    SELECT @ProdId, @ProdCode
end

并像

一样调用此方法
   SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=yourDbName;Integrated Security=True;Pooling=False");
            con.Open();
            SqlCommand cmd = new SqlCommand("GetProduct", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter ProdId = new SqlParameter("@ProdId", SqlDbType.Int);
            ProdId.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(ProdId);
            SqlParameter ProdCode = new SqlParameter("@ProdCode", SqlDbType.NVarChar,20);
            ProdCode.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(ProdCode);

            cmd.ExecuteNonQuery();
            string ProductCode = (string)cmd.Parameters["@ProdCode"].Value;
            int ProductId = Convert.ToInt32(cmd.Parameters["@ProdId"].Value);