如何在C#中的sql参数数组中获取sql参数类型输出值 [英] how to get sql parameter type output value in sql parameter array in C#

查看:243
本文介绍了如何在C#中的sql参数数组中获取sql参数类型输出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i也写了一个带类型输出的存储过程。



n



我将其他参数传递给存储过程,但如何获取输出类型的

参数。



存储过程:



Hi guys,

i have wrote an stored procedure with type output, as well.

n

i'm passing the other parameters to the stored procedure but how to get the
parameter which is of type output.

Stored Procedure:

GO
/****** Object:  StoredProcedure [dbo].[Sp_JobStep1]    Script Date: 07/20/2014 22:40:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Proc [dbo].[Sp_JobStep1]
(
      @JobName nvarchar(255)         
     ,@JobCode nvarchar(255)        
     ,@StartDate date       
     ,@EndDate date        
     ,@JobID int output
)
as

begin

insert into Job
	(
	JobName,
	JobCode,
	StartDate,
	EndDate
	)        
 select   
		  @JobName          
		 ,@JobCode         
		 ,@StartDate        
		 ,@EndDate         
		 
		 
select @JobID = @@IDENTITY from Job
     
end


b $ bc#code:





c# code:

SqlParameter[] Param = new SqlParameter[11];
                Param[0] = new SqlParameter("@JobName", Jobname);
                Param[1] = new SqlParameter("@JobCode", JobCode);
                Param[2] = new SqlParameter("@StartDate", strDate);
                Param[3] = new SqlParameter("@EndDate", endDate);
                Param[4] = new SqlParameter("@Client", Client);
                Param[5] = new SqlParameter("@JobDesc", JobDesc);
                Param[6] = new SqlParameter("@Rig", rig);
                Param[7] = new SqlParameter("@Well", well);
                Param[8] = new SqlParameter("@SectionID", Section);
                Param[9] = new SqlParameter("@hours", hours);
                Param[10] = new SqlParameter("@JobID", SqlDbType.Int);
                Param[10].Direction = ParameterDirection.Output;

                if (Section > 0)
                {
                    DAL.Insertion("Sp_JobStep1", Param);//DataAccessLayer
                }

                string JobID = Param["@JobID"].Value.ToString();//Error: Cannot implicitly convert string to int ["@JobID"]

//how to deal with the sql parameter of type 'output' n its value.





plzzz建议我。





谢谢



plzzz suggest me.


Thanks

推荐答案

嗨使用以下行。



Hi Use the below line.

string JobID = objSQLCommand.Parameters["@JobID"].Value.ToString()





希望这有帮助



Hope this helps


你好阿卜杜勒,你可以这样试试



Hi abdul,you can try it like this

param[4] = new SqlParameter("@JobID",SqlDbType.Int);
param[4].Direction = ParameterDirection.Output;


感谢Hard_Rockz在他的帮助下,我解决了这个问题。



我坚持使用我的代码参考#



Thanks to Hard_Rockz, with the help of him, i solved this issue.

i'm sticking my code for Ref#

try
           {
               string Jobname = txtjobname.Text;
               string JobCode = txtjobcode.Text;
               DateTime strDate = Convert.ToDateTime(txtsdate.Text);
               DateTime endDate = Convert.ToDateTime(txtedate.Text);
               string Client = txtclient.Text;
               string JobDesc = txtjobdescription.Text;
               int rig = Convert.ToInt32(ddlrig.SelectedValue);
               int well = Convert.ToInt32(ddlwell.SelectedValue);
               int Section = Convert.ToInt32(ddlSection.SelectedValue);
               string hours = txtehrs.Text;

               SqlParameter[] Param = new SqlParameter[11];
               Param[0] = new SqlParameter("@JobName", Jobname);
               Param[1] = new SqlParameter("@JobCode", JobCode);
               Param[2] = new SqlParameter("@StartDate", strDate);
               Param[3] = new SqlParameter("@EndDate", endDate);
               Param[4] = new SqlParameter("@Client", Client);
               Param[5] = new SqlParameter("@JobDesc", JobDesc);
               Param[6] = new SqlParameter("@Rig", rig);
               Param[7] = new SqlParameter("@Well", well);
               Param[8] = new SqlParameter("@SectionID", Section);
               Param[9] = new SqlParameter("@hours", hours);
               Param[10] = new SqlParameter("@JobID", SqlDbType.Int);
               Param[10].Direction = ParameterDirection.Output;

               if (Section > 0)
               {
                   DAL.Insertion("Sp_JobStep1", Param);
               }

               string JobID = Param[10].Value.ToString();//Solved

               Response.Redirect("JobStep2.aspx?JobID=" + JobID);

           }
           catch (Exception)
           {

               throw;
           }


这篇关于如何在C#中的sql参数数组中获取sql参数类型输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆