我想問如何將字符串轉換為整數,因為我嘗試了幾種轉換方法,但仍然有相同的錯誤.
我發現了錯誤,這是因為我嘗試在 Json Convert 中輸入整數. 但sql server表中需要整數.我怎樣才能讓它傳遞空值或使其變為0.有大大能幫忙嗎? 謝謝
Api 回應:
"Data":{
"name": "Tom",
"phonenumber" : "N/A"
}
或者
"Data":{
"name": "Tom",
"phonenumber" : null
}
編碼:
Dim listArray As String = JsonConvert.SerializeObject(jsonObject.Item("List"))
Dim name As String = If(JsonConvert.SerializeObject(jsonArray(j).SelectToken("Data.name")), Is Nothing, "", Replace(JsonConvert.SerializeObject(jsonArray(j).SelectToken("Data.name")), """", ""))
Dim phonenumber As Integer = if(JsonConvert.SerializeObject(jsonArray(j).SelectToken("Data.phonenumber")), Is Nothing, "", ,Replace(JsonConvert.SerializeObject(jsonArray(j).SelectToken("Data.phonenumber")), """", ""))
Dim con as SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
Dim sqlStr as String = "Insert Into tblPerson(Name, PhoneNumber) values(@name, @phonenumber)"
Dim cmd as SqlCommand = new SqlCommand(sqlStr, con)
Using cmd
cmd.Parameters.AddWithValue("@name", name)
cmd.Parameters.AddWithValue("@phonenumber", phonenumber)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Dim phonenumber As Integer = If(JsonConvert.SerializeObject(jsonArray(j).SelectToken("Data.phonenumber")), Nothing, "")