如何在JSON Web服务中的客户端数据库中存储数据

时间:2012-04-06 09:40:31

标签: asp.net json web-services

我有一个客户端页面,我有像empname,地址,性别等字段,如果我们单击Create按钮,Employee数据应该存储在Database ..为此,我写了一个类“EmployeeModel”和取字段写入属性,在客户端我添加了服务引用,在.aspx.cs我写了这样的东西:

    EmployeeModel emp = new EmployeeModel();
    emp.FirstName = txtFirstName.Text;
    emp.LastName = txtLastName.Text;
    emp.Address = txtAddress.Text;
    emp.DateOfBirth1 = txtDOB.Text;
    emp.Sex = rdbtnGender.SelectedItem.ToString();

之后我必须编写哪些步骤来存储DATABASE中的Employee数据。

1 个答案:

答案 0 :(得分:0)

首先,您需要使用以下javascript代码从客户端到服务器进行调用

$.ajax({
               type: "get",
               url: "?ajax_mode=SaveMyData,
               dataType: "json",
               async: false,
               data: { headerData: JSON.stringify(emp) },
               cache: false,
               success: function(data, textStatus) {
                   if (data.status == "success") {

                   }
                   else {

                   }
               }
           });

现在处理savemydata函数以保存员工信息

Public Sub SaveMyData()

    Dim objHeaderPostData As Dictionary(Of String, Object)
    Dim strParams As String = Request("headerData")
    Dim objOutput As New Ajax.BasicAjaxObjectOuput

    Dim jsS As New JavaScriptSerializer : objHeaderPostData = CType(jsS.DeserializeObject(strParams), Dictionary(Of String, Object))

    Dim strFirstName as string = CStr(objHeaderPostData.Item("FirstName"))
    '' get same way other infor from object objHeaderPostData



    Dim objOutput As New BasicOuputObject
    Dim objCommand As New SqlCommand
    Dim lstMyData As New List(Of MyData)
    Dim objMyData As MyData
    Dim objConn As New SqlConnection("Pass Connection String")
    objCommand.CommandText = '' write insert or update command as per requirement
    objCommand.Connection = objConn

    objConn.Open()
    objCommand.ExecuteScalar()



    objOutput.errorMessage = ""
    objOutput.obj = "Successfylly save record."
    objOutput.strMessage = "success"

    Dim objSerialiser As New System.Web.Script.Serialization.JavaScriptSerializer
    HttpContext.Current.Response.Write(objSerialiser.Serialize(objOutput))
    HttpContext.Current.Response.End()

End Sub

希望这会对你有所帮助。

有关json的更多信息,您可以访问我的博客here