What is the best practice for returning values from within a using statement? For instance:
using (SqlConnection Conn = new SqlConnection(s trConn))
{
SqlCommand cmd = new SqlCommand("spW hatever", Conn);
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@parm1", SqlDbType.VarCh ar, 128);
cmd.Parameters["@parm2"].Value = strOne;
Conn.Open();
string strReturn = cmd.ExecuteScal ar().ToString() ;
}
And then I would like to pass strReturn to another using statement to be a value for another query. Do I need to make each using statement into a method and then call that method? Such as:
....
string strReturn = cmd.ExecuteScal ar().ToString() ;
new Method(strRetur n);
}
using (SqlConnection Conn = new SqlConnection(s trConn))
{
SqlCommand cmd = new SqlCommand("spW hatever", Conn);
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@parm1", SqlDbType.VarCh ar, 128);
cmd.Parameters["@parm2"].Value = strOne;
Conn.Open();
string strReturn = cmd.ExecuteScal ar().ToString() ;
}
And then I would like to pass strReturn to another using statement to be a value for another query. Do I need to make each using statement into a method and then call that method? Such as:
....
string strReturn = cmd.ExecuteScal ar().ToString() ;
new Method(strRetur n);
}
Comment