sql server - Why i can not handle the store procedure return value is c#? -
i'm new in c#,and want write simple application work sql server store procedure,in sql server write store procedure:
use [mammutrecruitment] alter procedure [dbo].[firststep] @name nvarchar(max),@family nvarchar(max),@fathername nvarchar(max),@birthcertificate bigint,@placeofbirth nvarchar(max),@birthdate datetime, @nationalcode bigint,@religion nvarchar(max),@faith nvarchar(max),@nationality nvarchar(max),@bloodgroup nvarchar(max) begin declare @myid bigint insert [dbo].[usermainspecifications] values(@name,@family,@fathername,@birthcertificate,@placeofbirth,1,@birthdate,@nationalcode, @religion,@faith,@nationality,@bloodgroup,12,'123','123',1,2015-1-1,'12','123','1234',1) select @myid=[userid] [mammutrecruitment].[dbo].[usermainspecifications] [nationalcode]=@nationalcode select @myid myid end
, in c# write code call that:
using (sqlconnection con = new sqlconnection("data source=.;initial catalog=mammutrecruitment;integrated security=true")) { using (sqlcommand cmd = new sqlcommand("firststep", con)) { cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add("@name", sqldbtype.nvarchar).value = m.name; cmd.parameters.add("@family", sqldbtype.nvarchar).value = m.family; cmd.parameters.add("@fathername", sqldbtype.nvarchar).value = m.fathername; cmd.parameters.add("@birthcertificate", sqldbtype.bigint).value =convert.toint64(m.birthcertificate); cmd.parameters.add("@placeofbirth", sqldbtype.nvarchar).value = m.placeofbirth; cmd.parameters.add("@birthdate", sqldbtype.datetime).value =convert.todatetime(dt.tostring()); cmd.parameters.add("@nationalcode", sqldbtype.bigint).value =convert.toint64(m.nationalcode); cmd.parameters.add("@religion", sqldbtype.nvarchar).value = m.religion; cmd.parameters.add("@faith", sqldbtype.nvarchar).value = m.faith; cmd.parameters.add("@nationality", sqldbtype.nvarchar).value = m.nationality; cmd.parameters.add("@bloodgroup", sqldbtype.nvarchar).value = m.bloodgroup; sqlparameter retval = cmd.parameters.add("@myid", sqldbtype.bigint); retval.direction = parameterdirection.returnvalue; con.open(); cmd.executenonquery(); var retunvalue = cmd.parameters["@myid"].value;
in line 0 value always:
var retunvalue = cmd.parameters["@myid"].value;
happen?how can solve problem?thanks.
this line:
cmd.executenonquery();
is executing query , not returning value.
you start looking using instead:
cmd.executereader();
or if want value of first field of first row, use this:
var returnvalue = cmd.executescalar();
which give object can convert or cast appropriate type method.
Comments
Post a Comment