SQL Server to Webservice VB.net

Ronald Van Der Westhuizen 41 Reputation points
2020-12-10T08:08:21.95+00:00

Hi
i was wondering if possible
i want to extract the JSON data from SQL table and POST the JSON output to a Webservice

all this done using VB.net code

is this possible

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,375 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,714 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Albert Kallal 4,651 Reputation points
    2020-12-11T02:29:17.247+00:00

    Ok, it is assumed that you have a table or some table data.
    (quite much a "given").

    We want to take that data and convert the table data to JSON.

    So, say we have this;

    SELECT TOP 3 FirstName, LastName, HotelName FROM tblHotels ORDER BY ID  
    

    In sql server, this outputs this:

    47223-image.png

    Now, in VB, lets convert our table to JSON.

    We have this code:

           Dim strSQL As String  
            strSQL = "SELECT TOP 3 FirstName, LastName, HotelName FROM tblHotels ORDER BY ID"  
      
            Dim MyTable As DataTable = MyRst(strSQL)  
      
            Dim sBuf As String = JsonConvert.SerializeObject(MyTable).ToString  
      
            Debug.Print(sBuf)  
    

    OutPut:

    [{"FirstName":"Albert","LastName":"Kallal","HotelName":"My Cool"},{"FirstName":null,"LastName":null,"HotelName":"Radisson Inn Hotel"},{"FirstName":null,"LastName":null,"HotelName":"Ramada Inn #2"}]  
    

    So you can use the NewtonSoft (use nuget to install it).

    Thus any simple table data, or data in a list, or in fact even a single "row" of data you pull from SQL server into a table, row or even list object? NewtonSoft will "serialize' it into JSON for you.

    In fact, you can even pass NewtonSoft a class object - and it will do its magic.

    The next part + challenge is once you have the JSON is to make that web service call.

    You have to provide more details as to what the format and setup and requirementss and paramters of that web service in question is.

    So, use NewtonSoft for conversion - it a walk in the park, and as noted, be a class, a table, a list? Newsoft convert handles all of these formats. Including for some VERY STRANGE reason the built-in .net serlizers NOT supporting a dataRow type (don't know why???).

    Anyway - as you can see - getting "some thing" in JSON is easy - but setting up the web service call will in fact be your "real" challenge.

    If you give some details about the web service call, then I can/will follow up.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

  2. Duane Arnold 3,211 Reputation points
    2020-12-14T22:23:23.7+00:00

    Are you writing the Web service and Web client programs yourself?

    0 comments No comments