namespace HowTo.Samples.ADONET
{using System;
using System.Data;
using System.Data.SqlClient;public class outparamswithacommand
{
  public static void Main()
  {
    outparamswithacommand myoutparamswithacommand = new outparamswithacommand();
    myoutparamswithacommand.Run();
  }  public void Run()
  {
    String MsgString = null;    // Create a new Connection and SqlDataAdapter    SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");    // Create stored procedure with out parameter
    try
    {
      SqlCommand CreateProcCommand = new SqlCommand("CREATE PROCEDURE GetCompanyName  @CustomerId nchar(5), @CompanyName nchar(40) out as select @CompanyName = CompanyName from Customers where CustomerId = @CustomerId",myConnection);
      SqlCommand DropProcCommand = new SqlCommand("IF EXISTS (SELECT name FROM sysobjects WHERE name = 'GetCompanyName' AND type = 'P') DROP PROCEDURE GetCompanyName", myConnection);      myConnection.Open();
      DropProcCommand.ExecuteNonQuery();   // remove procedure if it exists
      CreateProcCommand.ExecuteNonQuery(); // create procedure      SqlCommand myCommand = new SqlCommand("GetCompanyName", myConnection);
      myCommand.CommandType = CommandType.StoredProcedure;      // Fill the parameters collection based upon stored procedure.
      SqlParameter workParam = null;      workParam = myCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5);
      // ParameterDirection.Input is the default for the Direction property. Thus the following line is not
      // needed here. To set the Direction property to its default value, use the following line.
      // workParam.Direction = ParameterDirection.Input;      workParam = myCommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 40);
      workParam.Direction = ParameterDirection.Output;      myCommand.Parameters["@CustomerID"].Value = "ALFKI";      myCommand.ExecuteNonQuery();
      MsgString = "CompanyName = " + myCommand.Parameters["@CompanyName"].Value.ToString();
    }
    catch(Exception e)
    {
      MsgString = e.ToString();
    }
    finally
    {
      myConnection.Close();
    }    Console.Write(MsgString);
  }
}

解决方案 »

  1.   

    就是这一句:
    MsgString = "CompanyName = " + myCommand.Parameters["@CompanyName"].Value.ToString();
      

  2.   

    CREATE PROCEDURE LoginUser
      @loginUN char(50) OUTPUT,
      @loginPW char(40)
    AS
    if @loginPW = (select [password] from users where username=@loginUN)
      return 0;
    else
      return -1;GO要loginUN返回值,把它定义成OUTPUT的,相应的SqlCommand的参数定义里也定义也ParameterDirect.InputOutput类型的,这样在存储过程里改变loginUN的值时,会返回出来。
    可以判断SqlCommand.ExecuteNonQuery()的返回值,为0表示成功,小于0表示失败。
      

  3.   

    代码中的是,
    cm.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int));
    cm.Parameters["ReturnValue"].Direction=ParameterDirection.ReturnValue;
    cm.ExecuteScalar();
    int result=Convert.ToInt32(cm.Parameters["ReturnValue"].Value.ToString());
    if(result==0)
    {
    ="ok";
    }
    else
    {
             ="error";
    }
    存储过程是
    if(...)
         return 0
    else
          return 1