ExecuteReader() v. ExecuteNonQuery()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    ExecuteReader() v. ExecuteNonQuery()

    It appears that you can only retrieve an Output parameter from a SQL Server
    stored procedure when using the ExecuteNonQuery of the SqlCommand class, and
    cannot use the ExecuteReader() method. In the code sample below, it bombs
    on the line with three *** when using the ExecuteReader, but works just fine
    when you use the ExecuteNonQuery . When using the ExecuteReader, the
    Output parameter appears to return a value of Null. The stored procedure
    was not changed inbetween.

    Suggestions? Thanks in advance.
    Mark

    String strConn =
    ConfigurationSe ttings.AppSetti ngs["myConnecti on"].ToString();
    SqlConnection sqlConn = new SqlConnection(s trConn);

    SqlCommand sqlComm = new SqlCommand("p_m y_proc", sqlConn);
    sqlComm.Command Type = CommandType.Sto redProcedure;

    sqlComm.Paramet ers.Add(new SqlParameter("@ bitSomeResult", SqlDbType.Bit)) ;
    sqlComm.Paramet ers["@bitSomeResult "].Direction = ParameterDirect ion.Output;

    sqlConn.Open();
    SqlDataReader dr = sqlComm.Execute Reader();
    //sqlComm.Execute NonQuery();
    String strSomeResult =
    sqlComm.Paramet ers["@bitSomeResult "].Value.ToString (); //***

    if (strSomeResult. ToLower() == "True".ToLower( ))
    {
    //Do something
    }
    else
    {
    //Do something else
    }

    sqlConn.Close() ;


  • Marina

    #2
    Re: ExecuteReader() v. ExecuteNonQuery ()

    I believe to retrieve output parameters after an executereader call, you
    have to get to the end of the reader, and I think close it.

    "Mark" <field027_nospa m_@umn.edu> wrote in message
    news:OXNcXL6SDH A.2852@tk2msftn gp13.phx.gbl...[color=blue]
    > It appears that you can only retrieve an Output parameter from a SQL[/color]
    Server[color=blue]
    > stored procedure when using the ExecuteNonQuery of the SqlCommand class,[/color]
    and[color=blue]
    > cannot use the ExecuteReader() method. In the code sample below, it bombs
    > on the line with three *** when using the ExecuteReader, but works just[/color]
    fine[color=blue]
    > when you use the ExecuteNonQuery . When using the ExecuteReader, the
    > Output parameter appears to return a value of Null. The stored procedure
    > was not changed inbetween.
    >
    > Suggestions? Thanks in advance.
    > Mark
    >
    > String strConn =
    > ConfigurationSe ttings.AppSetti ngs["myConnecti on"].ToString();
    > SqlConnection sqlConn = new SqlConnection(s trConn);
    >
    > SqlCommand sqlComm = new SqlCommand("p_m y_proc", sqlConn);
    > sqlComm.Command Type = CommandType.Sto redProcedure;
    >
    > sqlComm.Paramet ers.Add(new SqlParameter("@ bitSomeResult", SqlDbType.Bit)) ;
    > sqlComm.Paramet ers["@bitSomeResult "].Direction =[/color]
    ParameterDirect ion.Output;[color=blue]
    >
    > sqlConn.Open();
    > SqlDataReader dr = sqlComm.Execute Reader();
    > //sqlComm.Execute NonQuery();
    > String strSomeResult =
    > sqlComm.Paramet ers["@bitSomeResult "].Value.ToString (); //***
    >
    > if (strSomeResult. ToLower() == "True".ToLower( ))
    > {
    > //Do something
    > }
    > else
    > {
    > //Do something else
    > }
    >
    > sqlConn.Close() ;
    >
    >[/color]


    Comment

    Working...