问题
I am using SP in C# project to retrieve the output using SqlDataReader. Below is the code.
public List<LMTUsage> GetCompanyID(string userID, int roleId, String Organisation, String BusinessArea)
{
List<LMTUsage> objLMT = new List<LMTUsage>();
LMTUsage _oELMTUsage;
SqlConnection oCon = new SqlConnection(ConfigurationManager.ConnectionStrings["LMTConnectionString"].ConnectionString);
oCon.Open();
try
{
using (SqlCommand _oCmd = new SqlCommand())
{
_oCmd.Connection = oCon;
_oCmd.CommandType = CommandType.StoredProcedure;
_oCmd.CommandText = "[SC_GetDropdownValues]";
_oCmd.Parameters.Add(new SqlParameter("@UserId", userID));
_oCmd.Parameters.Add(new SqlParameter("@RoleId", roleId));
if (Organisation == "")
_oCmd.Parameters.Add(new SqlParameter("@Organisation", DBNull.Value));
else
_oCmd.Parameters.Add(new SqlParameter("@Organisation", Organisation));
if (BusinessArea == "")
_oCmd.Parameters.Add(new SqlParameter("@BusinessArea", DBNull.Value));
else
_oCmd.Parameters.Add(new SqlParameter("@BusinessArea", BusinessArea));
_oCmd.Parameters.Add(new SqlParameter("@Type", 3));
using (SqlDataReader _oRdr = _oCmd.ExecuteReader())
{
// _oRdr.Close();
while (_oRdr.Read())
{
_oELMTUsage = new LMTUsage();
_oELMTUsage.Company = _oRdr["Company"].ToString();
objLMT.Add(_oELMTUsage);
}
_oRdr.Close();
}
}
return objLMT;
}
catch (Exception Ex)
{
throw Ex;
}
//finally
//{
// oCon.Close();
// oCon.Dispose();
//}
}
It is very Simple SP with the select statement. SP return the output when executed from SQL 2014 but when implemented in the above method it doesn't return any output. Below is the screen for reference.
Please guide.
回答1:
Please check values in parameters. And use Dataset with fill method to retrive data from sp instead of using ExecuteReader().
回答2:
Unless your stored procedure is specifically named [SC_GetDropdownValues] (including the square brackets), you don't need to define square brackets in your SqlCommand's CommandText. Try:
_oCmd.CommandText = "SC_GetDropdownValues";
来源:https://stackoverflow.com/questions/51167794/sp-giving-output-but-sqldatareader-not