Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 反序列化json字符串并在SQL DB中插入_Vb.net - Fatal编程技术网

Vb.net 反序列化json字符串并在SQL DB中插入

Vb.net 反序列化json字符串并在SQL DB中插入,vb.net,Vb.net,我有一个函数,它从jsonblob获取json字符串,反序列化它并在dataview中返回。我想知道如何使用vb.net将数据视图中的数据插入sql数据库 我的职责如下 Public Function GetJsonString() As DataView Dim dsContactMeta As DataSet = Nothing Dim dv As DataView = Nothing 'url declaration for REST_ContactMeta

我有一个函数,它从jsonblob获取json字符串,反序列化它并在dataview中返回。我想知道如何使用vb.net将数据视图中的数据插入sql数据库

我的职责如下

Public Function GetJsonString() As DataView
    Dim dsContactMeta As DataSet = Nothing
    Dim dv As DataView = Nothing
    'url declaration for REST_ContactMeta
    Dim REST_ContactMetaURL As Uri = New Uri("http://jsonblob.com/api/jsonBlob/5420df11e4b00ad1f05ed29b")
    Dim strJSONData As String = ""
    Dim response As HttpWebResponse
    Dim reader As StreamReader

    Try
        Dim httpWebRequest As HttpWebRequest = WebRequest.Create(REST_ContactMetaURL)
        httpWebRequest.Method = WebRequestMethods.Http.Get
        httpWebRequest.ContentType = "application/json"

        response = httpWebRequest.GetResponse()
        reader = New StreamReader(response.GetResponseStream)

        strJSONData = reader.ReadToEnd
        strJSONData = "[" & strJSONData & "]" 'had to wrap the raw JSON data with brackets for parsing purposes
        Dim myXmlNode As System.Xml.XmlNode = JsonConvert.DeserializeXmlNode("{""root"":" + strJSONData + "}", "root") 'error is thrown if the JSON does not have a root element
        ' Dim myXmlNode As System.Xml.XmlNode = JsonConvert.DeserializeXmlNode(strJSONData)
        dsContactMeta = New DataSet("Test")
        Try
            dsContactMeta.ReadXml(New XmlNodeReader(myXmlNode))
            If dsContactMeta.Tables("root") Is Nothing Or dsContactMeta.Tables("root").Rows.Count = 0 Then
                Return Nothing 'in case of error return nothing
            Else
                dsContactMeta.Tables("root").Columns("job_id").SetOrdinal(0) 'setting the field name as the first column
                dv = New DataView
                dv.Table = dsContactMeta.Tables("root")
                Return dv
            End If
        Catch ex As Exception
            Return Nothing
        End Try
    Catch ex As Exception
        Return Nothing 'if for any reason the API has failed connecting to the server or other unknown error then return nothing
    End Try
End Function

将记录从DataView插入数据库,为此您需要读取DataView:

For Each rowView As DataRowView In dataView
    Debug.Write(item & " ")
    Dim row as DataRow  = rowView.Row;    
    // Do something //
    //insert into tablename(col1,col2,..) values(row['col1'],row['col2'],..)
Next