C# 为什么我需要一个局部变量来存储从Ajax到WCF方法的SqlParameter中的值

C# 为什么我需要一个局部变量来存储从Ajax到WCF方法的SqlParameter中的值,c#,jquery,ajax,wcf,C#,Jquery,Ajax,Wcf,我目前正在使用WCF和jQueryAjax进行测试。我是WCF的新手。无论如何,我有一个名为“ProductsService”的服务,它有四个从jQueryAjax调用的参数。我有点小问题,但谢天谢地我解决了。我的解决方案是通过估算得出的,那么任何人都能向我解释为什么这个问题会以这种方式解决吗 这里是问题场景 我将新产品从Ajax插入到WCF服务方法中,该方法将参数存储在SqlParameter中。但是,只有一个SqlParameter具有值,其余的SqlParameter为空。我稍微调整了一下

我目前正在使用WCF和jQueryAjax进行测试。我是WCF的新手。无论如何,我有一个名为“ProductsService”的服务,它有四个从jQueryAjax调用的参数。我有点小问题,但谢天谢地我解决了。我的解决方案是通过估算得出的,那么任何人都能向我解释为什么这个问题会以这种方式解决吗

这里是问题场景

我将新产品从Ajax插入到WCF服务方法中,该方法将参数存储在
SqlParameter
中。但是,只有一个
SqlParameter
具有值,其余的
SqlParameter
为空。我稍微调整了一下我的方法,但还是一样。然后我尝试添加局部变量,看看它是否会保存这些值,并将它们存储到
SqlParameter

这就是方法

public void insertProdect(int categoryId, string name, string discrption, decimal price)
    {
        // for debuge

        int t1 = categoryId;
        String t2 = name;
        String t3 = discrption;
        decimal t4 = price;


        String sc = ConfigurationManager.ConnectionStrings["BDCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(sc))
        {

            //
            SqlCommand cmd = new SqlCommand("spInsertNewProductByCategoryId", con);
            cmd.CommandType = CommandType.StoredProcedure;

            //
            SqlParameter CategoryId = new SqlParameter();
            CategoryId.ParameterName = "@CategoryId";
            //CategoryId.Value = categoryId;
            CategoryId.Value = t1;

            //
            SqlParameter ProductName = new SqlParameter();
            ProductName.ParameterName = "@ProductName";
            //ProductName.Value = ProductName;
            ProductName.Value = t2;

            //
            SqlParameter ProductDescription = new SqlParameter();
            ProductDescription.ParameterName = "@ProductDescription";
            //ProductDescription.Value = ProductDescription;
            ProductDescription.Value = t3;

            //
            SqlParameter ProductPrice = new SqlParameter();
            ProductPrice.ParameterName = "@ProductPrice";
            //ProductPrice.Value = price;
            ProductPrice.Value = t4; 

            //
            cmd.Parameters.Add(CategoryId);
            cmd.Parameters.Add(ProductName);
            cmd.Parameters.Add(ProductDescription);
            cmd.Parameters.Add(ProductPrice);

            //
            con.Open(); 
            cmd.ExecuteNonQuery();

        }
ajax调用

     $(document).on("click", "#doInsertNewProduct", function () {

    $.when(
        $.ajax({
            url: "../ProductsService.svc/insertProdect",
            method: "post",
            contentType: "application/json; charset=utf-8",

            data: JSON.stringify({
                categoryId: $("#InsertCatogeryTextName").val(),
                name: $("#InsertProductTextName").val(),
                discrption: $("#InsertProductTextDiscrption").val(),
                price: $("#InsertProductTextPrice").val()
            }),
            success: function () {
                location.reload();
                console.log("Done! Successfully insert new Product");
            },
            error: function () {
                console.log("Error! unbale to insert new Product");
            }
        }));

});

谢谢大家,您不需要使用局部变量,无论是单个
SqlParameter
s还是值。您的代码将更简单,如下所示:

public void InsertProduct(int categoryId, string name, string description, decimal price)
{
    String sc = ConfigurationManager.ConnectionStrings["BDCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(sc))
    {
        con.Open(); 
        using (SqlCommand cmd = new SqlCommand("spInsertNewProductByCategoryId", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CategoryId").Value = categoryId;
            cmd.Parameters.Add("@ProductName").Value = name;
            cmd.Parameters.Add("@ProductDescription").Value = description;
            cmd.Parameters.Add("@ProductPrice").Value = price;
            cmd.ExecuteNonQuery();
        }
    }
}
我强烈建议您也指定参数的类型,请注意,例如

cmd.Parameters.Add("@CategoryId", SqlDbType.Int).Value = categoryId;

您不需要使用局部变量来实现这一点-对于单个
SqlParameter
s或值。您的代码将更简单,如下所示:

public void InsertProduct(int categoryId, string name, string description, decimal price)
{
    String sc = ConfigurationManager.ConnectionStrings["BDCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(sc))
    {
        con.Open(); 
        using (SqlCommand cmd = new SqlCommand("spInsertNewProductByCategoryId", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CategoryId").Value = categoryId;
            cmd.Parameters.Add("@ProductName").Value = name;
            cmd.Parameters.Add("@ProductDescription").Value = description;
            cmd.Parameters.Add("@ProductPrice").Value = price;
            cmd.ExecuteNonQuery();
        }
    }
}
我强烈建议您也指定参数的类型,请注意,例如

cmd.Parameters.Add("@CategoryId", SqlDbType.Int).Value = categoryId;

这不需要局部变量。不幸的是,由于我们看不到不起作用的代码,我们无法说出它为什么不起作用…
ProductName.Value=name
而不是
ProductName.Value=ProductName
。这同样适用于
ProductDescription.Value=discrption
而不是
ProductDescription.Value=ProductDescription此操作不需要局部变量。不幸的是,由于我们看不到不起作用的代码,我们无法说出它为什么不起作用…
ProductName.Value=name
而不是
ProductName.Value=ProductName
。这同样适用于
ProductDescription.Value=discrption
而不是
ProductDescription.Value=ProductDescription