简体   繁体   English

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

[英]Why I need a local variables to store value in SqlParameter from Ajax to WCF method

I'm currently work with WCF and Jquery ajax for testing purposes. 我目前正在使用WCF和Jquery ajax进行测试。 I'm quite new with WCF. 我对WCF很陌生。 Anyways, I have a service is called "ProductsService" has four parameters that is invoked from Jquery Ajax. 无论如何,我有一个名为“ ProductsService”的服务,该服务具有从Jquery Ajax调用的四个参数。 I had a little problem with it, but thankfully I resolved it. 我对此有一点问题,但值得庆幸的是我解决了它。 My solution by estimation, so can any body explain to me why this problem solved this way? 我的解决方案是通过估算得出的,那么谁能向我解释为什么这个问题可以这样解决?

Here the problem scenario. 这里是问题场景。

I inserted new product from Ajax to WCF service method that will store the parameters in SqlParameter . 我将新产品从Ajax插入WCF服务方法,该方法将参数存储在SqlParameter However, only one SqlParameter has a value and the rest of SqlParameter were null. 但是,只有一个SqlParameter有一个值,其余SqlParameter为null。 I restructured my method a bit, but still the same. 我对方法进行了一些重组,但仍然相同。 Then I tried to add local variables to see if it would hold the values and store them into SqlParameter . 然后,我尝试添加局部变量以查看它是否可以保存值并将它们存储到SqlParameter

This is the method 这是方法

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 call 阿贾克斯电话

     $(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");
            }
        }));

});

Thanks all 谢谢大家

You don't need local variables for this - either for the individual SqlParameter s or the values. 您不需要为此使用局部变量-单个SqlParameter或值。 Your code would be simpler as: 您的代码将更简单:

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();
        }
    }
}

I'd strongly advise you to specify the types of the parameters as well, mind you, eg 强烈建议您也指定参数的类型,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM