Problem with calling a stored procedure from my c# code
I want to run a sp from my code. the sp name stored in a db. I pass the parameters via a dictionary. this code is one of my wcf service methods that host in a windows service. my code is :
private DataSet RunReport(int id, ParameterDictionary parametersDic)
{
DataSet result = new DataSet();
开发者_JAVA百科try
{
DataSet report = GetReportDataset(id);
if (report.Tables[0].Rows.Count > 0)
{
string reportType = GetReportTypeDataset(Convert.ToInt32(report.Tables[0].Rows[0]["ReportTypeID"]));
if (reportType != ReportType.StoredProcedure.ToString())
{
throw new Exception("your report is not a sp report.");
}
using (DbCommand paramCommand = DatabaseManager.Database.GetStoredProcCommand(report.Tables[0].Rows[0]["SQLQuery"].ToString()))
{
foreach (var parameter in parametersDic)
{
DatabaseManager.Database.SetParameterValue(paramCommand, parameter.Key, parameter.Value);
}
result = DatabaseManager.Database.ExecuteDataSet(paramCommand);
result.AcceptChanges();
}
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}
When I run this code the below error is accured:
An SqlParameter with ParameterName 'ID' is not contained by this SqlParameterCollection.
In addition, when I call this method via win forms every thing is ok. what is the problem?
I think you may have to add the parameters before setting them:
foreach param ...
{
DatabaseManager.Database.AddInParameter(paramCommand, <name>
, <DbType>
);
DatabaseManager.Database.SetParameterValue(paramCommand, parameter.Key, parameter.Value);
}
精彩评论