从没有输出参数的存储过程中获取返回值

时间:2012-10-31 16:57:15

标签: vb.net sql-server-2008 stored-procedures

我有一个vb.net应用程序,它使用应该返回值的存储过程将记录插入到db表中。此存储过程由其他人设置,最初链接到Web服务,我的应用程序通过该服务进行插入并在返回的xml中获得返回值。我现在可以访问db表,不知道如何在我的vb.net方法中接收返回值。

SQl存储过程代码段;

     @urlname varchar(500),
@siteid varchar(16),
@origin varchar(50),
@queryid varchar(25)
  AS
SET NOCOUNT ON;
declare @cnt int
declare @serverip varchar(16)
declare @mincnt int
declare @siteservercnt int
select @cnt=COUNT(*) from sites
      where urlname=@urlname
if @cnt = 0
    begin
    insert into sites (urlname,siteid,exported,origin,queryid)
    values(@urlname,@siteid,1,@origin,@queryid)
    select @siteservercnt = COUNT(*) from siteserverip where siteid=@siteid
    if @siteservercnt=0
       begin
            select top 1 @mincnt=COUNT(*),@serverip=serverip from siteserverip
        group by serverip
        order by COUNT(*)
        select top 1 @mincnt=sitecount,
                         @serverip=serverip from serveripcounts
        order by sitecount
        insert into siteserverip values(@siteid,@serverip)
        update serveripcounts set sitecount=sitecount+1
        where serverip=@serverip
        end
    end
SELECT siteid from sites
where urlname=@urlname
return 

和我的vb.net代码进行插入

        CommandObj.CommandText = "Getsite"
        CommandObj.CommandTimeout = 90

        Dim newUrl As String = String.Empty

        CommandObj.Parameters.Clear()

        Dim m_param As SqlParameter

        m_param = CommandObj.Parameters.Add("@urlname", SqlDbType.VarChar, 500)
        m_param.Direction = ParameterDirection.Input
        m_param.Value = name


        m_param = CommandObj.Parameters.Add("@siteid", SqlDbType.VarChar, 16)
        m_param.Direction = ParameterDirection.Input
        m_param.Value = siteid

        m_param = CommandObj.Parameters.Add("@origin", SqlDbType.VarChar, 50)
        m_param.Direction = ParameterDirection.Input
        m_param.Value = method

        m_param = CommandObj.Parameters.Add("@queryId", SqlDbType.VarChar, 25)
        m_param.Direction = ParameterDirection.Input
        m_param.Value = forumID


        Dim recordsAffected As Integer = CommandObj.ExecuteNonQuery

1 个答案:

答案 0 :(得分:2)

您可以使用ExecuteScalar获取该值。当您想要获取上次选择生成的值时,ExecuteNonQuery会返回受影响的行数。您也可以使用ExecuteReader,但是当您的SP可能返回更多列和/或更多行时,这很有用。

'Populate first column and first row value in siteID
Dim siteID As Integer = CommandObj.ExecuteScalar