0

我有一个客户端页面,我有像empname,address,sex等字段,如果我们点击创建按钮,员工数据应该存储在数据库中。为此,我编写了一个类“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();

之后我必须编写哪些步骤才能将员工数据存储在数据库中。

4

1 回答 1

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 的更多信息,您可以在此处访问我的博客

于 2012-04-06T10:11:07.327 回答