0
我有這個包含兩個外部對象(PtRegData和PartnerData)的嵌套JSON。 PtRegData是單記錄數據,另一方面大部分時間PtRegData有許多與PtRegData相關的記錄。基本上是一對多的關係。在Vb.net中同時使用兩個不同的存儲過程保存到兩個不同的表中
我的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": "[email protected]",
"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
}]
}}
我使用兩種不同的webMethods處理的服務器端這個數據如下
我自動實現屬性,
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
對於第一個數據集,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
我怎樣才能在一個結合了的Webmethods?
PS,第一條記錄的火災只有一次,而第二個是一個循環,需要火存儲過程不止一次