我有这个嵌套的JSON,它包含两个外部对象(PtRegData和PartnerData)。 PtRegData是单记录数据,另一方面PtRegData大部分时间都有许多与PtRegData相关的记录。基本上是一对多的关系。
我的JSON
{
"PtRegData": {
"RecID": "1277",
"filenum": "15090248",
"FnameTxt": "Ahmad",
"Snametxt": "Morsi",
"Tnametxt": "abdul fattah",
"Lnametxt": "Abu Maizer",
"FilStatTxt": "2",
"SexTxt": "1",
"MarStatTxt": "4",
"NatIDTxt": "18",
"OcupID": "15",
"SSnum": "6666888",
"Passnum": "",
"TelMob": "(88) 888-83333",
"TelLnd": "",
"TelInt": "",
"TelWrk": "",
"WrkExt": "",
"autocomplete": "15549 Paramount Boulevard, Paramount, CA, United States",
"Email": "embryologist85@gmail.com",
"street_number": "12323",
"route": "Dorothy Street",
"AptNo": "22",
"locality": "Los Angeles",
"administrative_area_level_1": "CA",
"postal_code": "90049",
"country": "United States",
"DobTxt": "12/31/1985",
"AddrCtryID": "6",
"AddrStateID": "1589",
"PartnerData": [{
"RecID": 2383,
"PrtStatus": 1
}, {
"RecID": 3387,
"PrtStatus": 0
}, {
"RecID": 3388,
"PrtStatus": 0
}]
}}
我使用两种不同的web方法在服务器端处理这些数据,如下所示
我的自动实施属性,
Public Class PartnerData
Public Property RecID As Integer
Public Property PrtStatus As Integer
Public Property ptfilenum As Integer
Public Property prfilenum As Integer
End Class
Public Class PtRegData
Public Property RecID As Integer
Public Property filenum As Integer
Public Property FnameTxt As String
Public Property PartnerData As PartnerData()
End Class
Public Class TblReg
Public Property PtRegData As PtRegData
End Class
对于First Dataset,PtRegData
<WebMethod> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function TblRegRecHdlr(ByVal PtRegData As List(Of PtRegData))
Try
Dim constr As String = ConfigurationManager.ConnectionStrings("ARTSQLConStrng").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("TblRegDynRecHdlr", con)
cmd.CommandType = CommandType.StoredProcedure
If (String.IsNullOrEmpty(PtRegData(0).RecID)) Then
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = Convert.ToInt32(PtRegData(0).RecID)
End If
If (String.IsNullOrEmpty(PtRegData(0).filenum)) Then
cmd.Parameters.Add("@FileNum", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@FileNum", SqlDbType.Int).Value = Convert.ToInt32(PtRegData(0).filenum)
End If
If (String.IsNullOrEmpty(PtRegData(0).FnameTxt)) Then
cmd.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = DBNull.Value
Else
cmd.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = PtRegData(0).FnameTxt
End If
Dim dt As New DataTable()
Using sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
End Using
'Dim data = PtRegData(0).PartnerData
'TblRegJoinRecHdlr(data)
Dim jsondata As String = JsonConvert.SerializeObject(dt)
Return jsondata ' I m returning value from SQL server, Crucial
End Using
End Using
Catch ex As Exception
Return ex.Message
End Try
End Function
对于第二个数据集PtRegData
<WebMethod> _
Public Shared Function TblRegJoinRecHdlr(ByVal Data) As List(Of PartnerData)
Dim constr As String = ConfigurationManager.ConnectionStrings("ARTSQLConStrng").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("TblRegJoinDynHdlr", con)
cmd.CommandType = CommandType.StoredProcedure
con.Open()
For i As Integer = 0 To Data.Count - 1
cmd.Parameters.Clear()
If (String.IsNullOrEmpty(Data(i).RecID)) Then
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = Convert.ToInt32(Data(i).RecID)
End If
If (String.IsNullOrEmpty(Data(i).ptfilenum)) Then
cmd.Parameters.Add("@ptfilenum", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@ptfilenum", SqlDbType.Int).Value = Convert.ToInt32(Data(i).ptfilenum)
End If
If (String.IsNullOrEmpty(Data(i).prfilenum)) Then
cmd.Parameters.Add("@prfilenum", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@prfilenum", SqlDbType.Int).Value = Convert.ToInt32(Data(i).prfilenum)
End If
cmd.Parameters.Add("@PrtStatus", SqlDbType.Int).Value = (Data(i).PrtStatus)
cmd.ExecuteNonQuery()
Next
con.Close()
End Using
End Using
Return Nothing
End Function
如何将两种Web方法合并为一种?
PS,第一条记录只触发一次,而第二条记录是循环,需要多次触发存储过程
答案 0 :(得分:0)
所以我以这种方式结合它们
<WebMethod> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function TblRegRecHdlr(ByVal PtRegData As List(Of PtRegData))
Dim jsondata As String
Try
Dim constr As String = ConfigurationManager.ConnectionStrings("ARTSQLConStrng").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("TblRegDynRecHdlr", con)
cmd.CommandType = CommandType.StoredProcedure
If (String.IsNullOrEmpty(PtRegData(0).RecID)) Then
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = Convert.ToInt32(PtRegData(0).RecID)
End If
If (String.IsNullOrEmpty(PtRegData(0).filenum)) Then
cmd.Parameters.Add("@FileNum", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@FileNum", SqlDbType.Int).Value = Convert.ToInt32(PtRegData(0).filenum)
End If
If (String.IsNullOrEmpty(PtRegData(0).FnameTxt)) Then
cmd.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = DBNull.Value
Else
cmd.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = PtRegData(0).FnameTxt
End If
Dim dt As New DataTable()
Using sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
End Using
jsondata = JsonConvert.SerializeObject(dt)
End Using
Using cmd As New SqlCommand("TblRegJoinDynHdlr", con)
cmd.CommandType = CommandType.StoredProcedure
Dim Data = PtRegData(0).PartnerData
con.Open()
For i As Integer = 0 To Data.Count - 1
cmd.Parameters.Clear()
If (String.IsNullOrEmpty(Data(i).RecID)) Then
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@RecID", SqlDbType.Int).Value = Convert.ToInt32(Data(i).RecID)
End If
If (String.IsNullOrEmpty(Data(i).ptfilenum)) Then
cmd.Parameters.Add("@ptfilenum", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@ptfilenum", SqlDbType.Int).Value = Convert.ToInt32(Data(i).ptfilenum)
End If
If (String.IsNullOrEmpty(Data(i).prfilenum)) Then
cmd.Parameters.Add("@prfilenum", SqlDbType.Int).Value = DBNull.Value
Else
cmd.Parameters.Add("@prfilenum", SqlDbType.Int).Value = Convert.ToInt32(Data(i).prfilenum)
End If
cmd.Parameters.Add("@PrtStatus", SqlDbType.Int).Value = (Data(i).PrtStatus)
cmd.ExecuteNonQuery()
Next
con.Close()
End Using
Return jsondata
End Using
Catch ex As Exception
Return ex.Message
End Try
End Function
&#13;