How to call stored procdure and return different type value?


Author
Message
Raymond Lo
Raymond Lo
StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)StrataFrame Beginner (46 reputation)
Group: Forum Members
Posts: 18, Visits: 101
Dear All,

      I have created a stored procdure name sp_GetInfoCurrency  and the codeing as bellow:

CREATE PROCEDURE [dbo].[sp_GetInfoCurrency]
 @pcurcode nchar(5),
 @pcurid int output,
 @pcurname nvarchar(50) output,
 @pcurrate smallmoney output
AS
BEGIN
 SET NOCOUNT ON;

 select @pcurid=curid, @pcurname=curname, @pcurrate=currate from currency where curcode=@pcurcode
 if @pcurid is null
 begin
  set @pcurid=0
  set @pcurname=''
  set @pcurrate=0
 end
END

         My question is, How to call the stored procdure through StrataFrame, Is there any method or fuctions easy to do this?
         Please provide the sample code.

Thanks.

Reply
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Raymond,



This is very easy! The trick is to use parameters to get the return data. I'm just typing this, so the enums for CommandType, Direction and SqlDataType aren't exactly accurate (intellisense will fix 'em right up though!).



'-- Assumes that pcurcode is provided via a method argument

Using cmd As New SqlCommand()

  '-- Setup sproc

  cmd.CommandType = StoredProcedure

  cmd.CommandText = "sp_GetInfoCurrency"



  '-- Add input param

  cmd.Parameters.Add("@pcurcode",char,5)

  cmd.Parameters("@pcurcode").Value = pcurcode



  '-- Add output params

  cmd.Parameters.Add("@pcurid",int).Direction = Output

  cmd.Parameters.Add("@pcurname",nvarchar,50).Direction = Output

  cmd.Parameters.Add("@pcurrate",smallmoney).Direction = Output



  '-- Execute spoc

  Me.ExecuteNonQuery(cmd)



  '-- Get output via params

  ' Note that you might need to check for NULLs here too

  ' (I'm not here though)

  ' Vars declared outside of Using someplace....

  pcurid = CType(cmd.Parameters("@pcurid").Value, Integer)

  pcurname = Ctype(cmd.Parameters("@pcurname").Value, String)

  pcurrate = CType(cmd.Parameters("@pcurrate").Value, Decimal)

End Using

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search