"参数化查询需要参数,该参数未提供。"但它供应

时间:2017-10-19 21:46:02

标签: c# parameterized-query

public void InsertBaselineDetailsToExistingBaseline(BaselineSummary summary)
{
    var sqlConnection = ConfigurationManager.ConnectionStrings["PdsLoadTesting"].ConnectionString;

    if (summary.BaselineId != null)
    {
        using (var connection = new SqlConnection(sqlConnection))
        {
            connection.Open();

            using (var baselineDetailCommand = connection.CreateCommand())
            {
                baselineDetailCommand.CommandText = "INSERT INTO dbo.baselinedetail (BaselineDetailId, BaselineId, Endpoint, PostData, RequestMethod, ComparisonType, InactiveTest, RunOrder, ScenarioDescription, EndpointPattern) VALUES (@BaselineDetailId, @BaselineId, @Endpoint, @PostData, @RequestMethod, @ComparisonType, 0, @RunOrder, @ScenarioDescription, @EndpointPattern)";

                baselineDetailCommand.Parameters.Add("@BaselineDetailId", SqlDbType.UniqueIdentifier);
                baselineDetailCommand.Parameters.Add("@BaselineId", SqlDbType.UniqueIdentifier);
                baselineDetailCommand.Parameters.Add("@Endpoint", SqlDbType.VarChar);
                baselineDetailCommand.Parameters.Add("@PostData", SqlDbType.VarChar);
                baselineDetailCommand.Parameters.Add("@RequestMethod", SqlDbType.VarChar);
                baselineDetailCommand.Parameters.Add("@ComparisonType", SqlDbType.VarChar);
                baselineDetailCommand.Parameters.Add("@RunOrder", SqlDbType.Int);
                baselineDetailCommand.Parameters.Add("@ScenarioDescription", SqlDbType.Int);
                baselineDetailCommand.Parameters.Add("@EndpointPattern", SqlDbType.VarChar);

                foreach (var baselineDetail in summary.Details)
                {
                    baselineDetailCommand.Parameters["@BaselineDetailId"].Value = Guid.NewGuid();
                    baselineDetailCommand.Parameters["@BaselineId"].Value = summary.BaselineId;
                    baselineDetailCommand.Parameters["@Endpoint"].Value = baselineDetail.Endpoint;
                    baselineDetailCommand.Parameters["@PostData"].Value = string.IsNullOrEmpty(baselineDetail.PostData) ? (object)DBNull.Value : baselineDetail.PostData;
                    baselineDetailCommand.Parameters["@RequestMethod"].Value = baselineDetail.RequestMethod;
                    baselineDetailCommand.Parameters["@ComparisonType"].Value = baselineDetail.ComparisonType;
                    baselineDetailCommand.Parameters["@RunOrder"].Value = (object)baselineDetail.RunOrder ?? DBNull.Value;
                    baselineDetailCommand.Parameters["@ScenarioDescription"].Value = baselineDetail.ScenarioDescription;
                    baselineDetailCommand.Parameters["@EndpointPattern"].Value = baselineDetail.EndpointPattern;

                    baselineDetailCommand.ExecuteNonQuery();
                }
            }
        }
    }
}

当我尝试运行上面的方法时出现错误

  

参数化查询需要参数@PostData,该参数未提供。

提供了参数,如果参数确实为null,我也检查null并传递DBNull.Value。在这种情况下,Null是@PostData的有效值。

有关为何此方法仍然返回此错误的任何建议?

0 个答案:

没有答案