简体   繁体   English

使用Ajax Webmethod将Json对象插入SQL Server 2008

[英]Insert Json Object into SQL Server 2008 using Ajax Webmethod

function AddJSon() {
    var jsonArr = [];
    for (var i = 1; i < 4; i++) {
        for (var j = 0; j < 3; j++) {
            var ProductId = $("#hfProductId" + i + "_" + j).val();
            if (ProductId != undefined) {
                jsonArr.push({
                    SalesOrderItemId: $("#hfProductId" + i + "_" + j).val(),
                    AttachmentCode: $("#txtAttachmentCode" + i + "_" + j).val(),
                    AttachmentName: $("#txtAttachName" + i + "_" + j).val(),
                    Qty: $("#txtAttachmentQty" + i + "_" + j).val(),
                    UnitCost: $("#txtAttachCost" + i + "_" + j).val(),
                    TotalCost: $("#txtAttachmentTotalCost" + i + "_" + j).val(),
                })
            }
        }
    }
}

In Above code i have pushed the values of various fields in Json array and now i want to save them in database without using loop. 在上述代码中,我已将Json数组中各个字段的值压入,现在我想将其保存在数据库中而不使用循环。

I will step down the thing you want to do next. 接下来,我将辞职。

  1. Since you have already got your JSON you can create a web method in required page's asxp.cs.(I named it AddJSon()) 由于您已经有了JSON,因此可以在所需页面的asxp.cs中创建一个Web方法。(我将其命名为AddJSon())

  2. This method should accept a parameter type of the JSON format what we already have.So create a class for that.(I named it JsonDataList) Your request objects should be as follows. 此方法应接受我们已经拥有的JSON格式的参数类型。因此,为此创建一个类。(我将其命名为JsonDataList)您的请求对象应如下所示。

     public class JsonDataList { public List<JsonData> jsonData { get; set; } } public class JsonData { public int SalesOrderItemId { get; set; } public string AttachmentCode { get; set; } public string AttachmentName { get; set; } public int Qty { get; set; } public decimal UnitCost { get; set; } public decimal TotalCost { get; set; } } 
  3. Then you have to create your table for this.I assume you are using MSSQL and you would be able to create your table structure accordingly.(connection strings etc. too) 然后必须为此创建表。我假设您正在使用MSSQL,并且您将能够相应地创建表结构(连接字符串等)。

  4. When part 3 is done, Your web method should be changed like following to insert data to created table.(i will do it in the web method itself which is not good) 完成第3部分后,应像下面所述更改您的Web方法以将数据插入到创建的表中。(我会在Web方法本身中这样做,这不好)

     [WebMethod] public static string AddJSon(JsonDataList jsonDataList) { try { using(SqlConnection con=new SqlConnection("your_connection_String")) { con.Open(); foreach(JsonData data in jsonDataList) { string query = "INSERT into table (column1,column2,column3,...) VALUES (@value1,@value2,@value3,..)"; SqlCommand command = new SqlCommand(query, con); command.Parameters.Add("@value1",data.SalesOrderItemId); command.Parameters.Add("@value2",data.AttachmentCode ); command.Parameters.Add("@value3",data.AttachmentName ); . . command.ExecuteNonQuery(); } } return "Success"; }catch(Exception e) { return "Error"; } } 

hope this might be helpful for you.This doesn't have any validations or any good practices.Just get the idea from this. 希望这对您有所帮助。没有任何验证或良好实践。

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

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