存储过程
Declare @NSQL nvarchar(4000)
SET @NSQL='select @AllRecord=count(distinct EmployeeID) from Employee'
exec sp_executesql @NSQL,N'@AllRecord int output',@AllRecord out          --能到到@AllRecord 的值
c#
string NSQL="";
int AllRecord =0;
NSQL='select @AllRecord=count(distinct EmployeeID) from Employee'   //这里怎么获取值给AllRecord  不要再单独写个存储过程 里面加个output参数 有办法吗?

解决方案 »

  1.   

    string NSQL = "'select count(distinct EmployeeID) from Employee";
    int AllRecord = 0;
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "...";
    SqlCommand cmd = new SqlCommand(NSQL, conn);
    try
    {
    conn.Open();
    object obj = cmd.ExecuteScalar();
    if (obj != null)
    {
    AllRecord = Convert.ToInt32(obj);
    }
    }
    catch (Exception ex)
    {}
    finally
    {
    if (conn.State == ConnectionState.Open)
    {
    conn.Close();
    }
    }
      

  2.   

    string NSQL = "'select count(distinct EmployeeID) from Employee";
    改成
    string NSQL = "select count(distinct EmployeeID) from Employee";
    笔误
      

  3.   


    arrpara[0] = new SqlParameter("@AllRecord", SqlDbType.Int);
    arrpara[0].Value = ID;
    arrpara[0].Direction = ParameterDirection.Output;
    SqlConnection con=new ...();
    ...
    SqlCommand cmd=new ...();
    cmd.Commandtype=CommandType.StoredProcedure;
    int Result = int.Parse(arrpara[0].Value.ToString());