2015-10-13 169 views
0

我在C#和SQL Server初学者,我写了这个查询创建SQL Server中的存储过程:我如何获得SQL Server存储过程在c#中的返回值?

create procedure newBehzad 
    @id bigint 
as 
    DECLARE @ResultValue int 

    select * 
    from TABLEA 
    where id > @id 

    SET @ResultValue = -5 
go 

一切工作,和我写了这个C#代码来调用存储过程,它返回一个值:

using (var conn = new SqlConnection(connectionString)) 
using (var command = new SqlCommand("newBehzad", conn) 
{ 
    CommandType = CommandType.StoredProcedure 
}) 
{ 
    conn.Open(); 

    command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2; 
    command.Parameters.Add("@ResultValue", SqlDbType.Int); 

    SqlParameter retval = command.Parameters.Add("@ResultValue", SqlDbType.Int); 
    retval.Direction = ParameterDirection.ReturnValue; 

    retunvalue = (string)command.Parameters["@ResultValue"].Value; 

    //SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar); 
    command.ExecuteNonQuery(); 
    conn.Close(); 
} 

MessageBox.Show(returnValue); 

但是当我运行的C#Windows应用程序,我得到这个错误:

Procedure or function newBehzad has too many arguments specified.

我该如何解决呢?谢谢。

+0

ResultValue不是存储过程的参数。存储的产品是做什么的?它选择数据,但是您执行非查询。无义。 –

+0

@GiorgiNakeuri谢谢你的回答,在我的TABELA上有一个id标识(1,1)字段,当选择那个TABLEA时,返回最新的id – user3671271

回答

1

改变你程序进行:

create procedure newBehzad @id bigint, @ResultValue int OUT 
as 
SET @ResultValue = 0 
BEGIN 
    select *from TABLEA 
    where id>@id 
    SET @ResultValue = -5 
END 
go 

请尝试somethink这样的:

object returnValue = null; 
      using (var conn = new System.Data.SqlClient.SqlConnection(AbaseDB.DBFactory.GetInstance().GetConnectionString())) 
      { 
       using (System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("newBehzad", conn) { CommandType = CommandType.StoredProcedure }) 
       { 
        conn.Open(); 
        command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2; 
        command.Parameters.Add("@ResultValue", SqlDbType.Int).Direction = ParameterDirection.Output; 

        command.ExecuteNonQuery(); 

        returnValue = command.Parameters["@ResultValue"].Value; 

        conn.Close(); 
       } 
       if (returnValue != null) 
        MessageBox.Show(returnValue.ToString()); 
      } 
+0

感谢您的回答我得到ExecuteScala()引用错误,那是什么引用? – user3671271

+0

System.Data(w System.Data.dll) System.Data.SqlClient(w System.Data.SqlClient.dll) – Arkadiusz

+0

我收到此错误:其他信息:指定的转换无效。 – user3671271

1
using (var conn = new SqlConnection(connectionString)) 
      using (var command = new SqlCommand("newBehzad", conn) 
      { 
       CommandType = CommandType.StoredProcedure 
      }) 
      { 
       conn.Open(); 
       command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2; 
       // command.Parameters.Add("@ResultValue", SqlDbType.Int); Comment this line 


       SqlParameter retval = command.Parameters.Add("@ResultValue", SqlDbType.Int); 
       retval.Direction = ParameterDirection.ReturnValue; 

       retunvalue = (string)command.Parameters["@ResultValue"].Value; 

       //SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar); 
       command.ExecuteNonQuery(); 
       conn.Close(); 
      } 
      MessageBox.Show(returnValue); 
+0

创建我的freind,但是存储过程返回null,为什么? – user3671271

1

所有你需要改变存储过程返回值首先:

create procedure newBehzad @id bigint 
as 
    DECLARE @ResultValue int 
    select *from TABLEA 
    where id>@id 
    SET @ResultValue = -5 

    Return @ResultValue 
go 

然后抓住它:

using (var conn = new SqlConnection(connectionString)) 
{ 
    conn.Open();   

    using (var cmd = new SqlCommand("newBehzad", conn) 
    { 
     cmd.CommandType = CommandType.StoredProcedure; 

     SqlParameter retval = new SqlParameter(); 
     retval.Direction = ParameterDirection.ReturnValue; 

     cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = 2; 
     cmd.Parameters.Add(retval); 

     cmd.ExecuteNonQuery(); 

     returnValue = (int)retval.Value; 
    } 
} 

但我真的不能让你为什么在存储过程中选择数据...

+0

请写下你的C#代码完整 – user3671271

+0

查看修改...... –