POST HTTP request that sends SQL data to a site

Would there be a way to create a POST HTTP request that sends SQL data to a site and then a GET request to show what they received?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well, the question is not really different is can you pass some values to some sub you have in code? Well, gee? The answer is does that simple sub in code support or have parameters that allows you to do this?

I mean, do you think you can pass SQL to Amazon.com when ordering a book, and order a book for me?

So the answer is sure, you can do this. But on the web site, you have to write code (usually called a web method) that accepts that SQL and then does something with the sql (like say hit or update or pull data from say the sql server that drives that web site).

So you can certainly create web methods on that web site. But you certainly can’t just out of the blue decide to send some SQL to your web site anymore then can you decide to send some SQL updates to your on-line banking application and give yourself a really nice Christmas bonus!

So the simple answer?
Yes, you can send SQL commands to a site, but that site will have to be setup with some web service methods that can accept what you want to send.

So, say I have a web forms site. And I want to get allow me to send a query to that web site?

Ok, so we can drop in this public function into that web page as a code stub.

It could look like this:

<WebMethod()>
Public Shared Function GetSQLresults(strSQL As String, MyColumn As String) As String

    Dim strResult As String = ""

    Using cmdSQL As New SqlCommand(strSQL,
            New SqlConnection(My.Settings.Test3))

        Dim MyTable As New DataTable
        cmdSQL.Connection.Open()
        Try
            MyTable.Load(cmdSQL.ExecuteReader)
            If MyTable.Rows.Count > 0 Then
                strResult = MyTable.Rows(0).Item(MyColumn)
            Else
                strResult = "no data"
            End If

        Catch ex As Exception
            strResult = Err.Description
        End Try

    End Using

    Return strResult

End Function

So, now you can call that method. Say as jQuery, you can do this:
     <script>
            function mywebcall() {
                alert('start');

            var strSQL = "SELECT ID, HotelName, City from tblHotels";
            var MyColumn = "HotelName";

            $.ajax({
                type: "POST",
                url: 'ListViewTest.aspx/GetSQLresults',
                data: JSON.stringify({ strSQL : strSQL, MyColumn : MyColumn}),
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                success: function (em) {
                    alert('SQL result = ' + em.d);
                }
            });
            }

        </script>

Now, as a general rule, I would not suggest that you setup some web service calls to just accept SQL and do anything you want. So, you can certainly setup and write public functions on that web site, and those public functions (or subs if you not returning values) can accept parameters.

In fact, you could with the above method placed in a web service asmx page?

you could type in this in the URL:

https://localhost:44316/WebService1.asmx/
GetSQLresults?strSQL=SELECT ID, HotelName from tblHotels WHERE ID = 20&MyColumn=HotelName

And the web site would spit out this:
<string xmlns="http://mywebdata.org/">My Cool</string>

Or if you ran that JavaScript (jQuery), then the output would be this:

POST HTTP request that sends SQL data to a site

Of course you use some kind of web service call – that way you get a “object” back with parameters that you can easily use in code.

As noted, be it amazon.com or YOUR web site? Some security concerns would exist here to allow any old SQL to be run here. But the general idea and concept is that you can and will and should create web service calls.

The nice thing about asp.net web methods like above?
Without any extra work they accept:

SOAP 1.1 calls
SOAP 1.2 calls
REST with post headers
REST with URL parameters like above last example.
(so the REST calls can use a simple post for above - with parameters in url)

And the web service can (and will) return JSON format data, or XML format. As per above, the jQuery ajax called passed and returned JSON data. And the REST URL with ?, & parameters sent returned xml. And all of the 4 formats (json, xml) and SOAP or rest all work without any more additional code then what I posted here.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x