2012-04-06 33 views
0

我有一个客户端页面,并且我有像empname,地址,性别等字段,如果我们单击Create按钮Employee数据应该存储在数据库中。为此我写一个类“EmployeeModel”,并采取书面领域的性能和在客户端我添加服务引用和在.aspx.cs我writeen是这样的:如何将数据存储在JSON Web服务中的客户端数据库中

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中。

回答

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

+0

使用aspx页面就可以做什么?我们不想使用AJAX .. – 2012-04-06 10:48:44

+0

yes..its使用jQuery ..u可以免费下载并添加到您的aspx页面。 – 2012-04-06 10:57:21

相关问题