| 
 | 
| Imports Microsoft.VisualBasic
 | 
| Imports System.Web
 | 
| Imports System.Threading
 | 
| Imports System.Data.SqlClient
 | 
| 
 | 
| Public Class SapQ
 | 
|     Implements IHttpAsyncHandler
 | 
| 
 | 
|     Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
 | 
|         Get
 | 
|             Return False
 | 
|         End Get
 | 
|     End Property
 | 
| 
 | 
|     Public Function BeginProcessRequest( _
 | 
|         ByVal context As System.Web.HttpContext, _
 | 
|         ByVal cb As System.AsyncCallback, _
 | 
|         ByVal extraData As Object) _
 | 
|         As System.IAsyncResult _
 | 
|         Implements System.Web.IHttpAsyncHandler.BeginProcessRequest
 | 
| 
 | 
|         Dim asynch As New AsynchOperation(cb, context, extraData)
 | 
|         asynch.StartAsyncWork()
 | 
|         Return asynch
 | 
| 
 | 
|     End Function
 | 
| 
 | 
|     Public Sub EndProcessRequest(ByVal result As  _
 | 
|          System.IAsyncResult) _
 | 
|          Implements System.Web.IHttpAsyncHandler.EndProcessRequest
 | 
|     End Sub
 | 
| 
 | 
|     Public Sub ProcessRequest(ByVal context _
 | 
|             As System.Web.HttpContext) _
 | 
|             Implements System.Web.IHttpHandler.ProcessRequest
 | 
|         Throw New InvalidOperationException()
 | 
|     End Sub
 | 
| End Class
 | 
| 
 | 
| Class AsynchOperation
 | 
|     Implements IAsyncResult
 | 
|     Private _completed As Boolean
 | 
|     Private _state As [Object]
 | 
|     Private _callback As AsyncCallback
 | 
|     Private _context As HttpContext
 | 
| 
 | 
|     ReadOnly Property IsCompleted() As Boolean _
 | 
|             Implements IAsyncResult.IsCompleted
 | 
|         Get
 | 
|             Return _completed
 | 
|         End Get
 | 
|     End Property
 | 
| 
 | 
|     ReadOnly Property AsyncWaitHandle() As WaitHandle _
 | 
|             Implements IAsyncResult.AsyncWaitHandle
 | 
|         Get
 | 
|             Return Nothing
 | 
|         End Get
 | 
|     End Property
 | 
| 
 | 
|     ReadOnly Property AsyncState() As [Object] _
 | 
|             Implements IAsyncResult.AsyncState
 | 
|         Get
 | 
|             Return _state
 | 
|         End Get
 | 
|     End Property
 | 
| 
 | 
|     ReadOnly Property CompletedSynchronously() As Boolean _
 | 
|             Implements IAsyncResult.CompletedSynchronously
 | 
|         Get
 | 
|             Return False
 | 
|         End Get
 | 
|     End Property
 | 
| 
 | 
|     Public Sub New(ByVal callback As AsyncCallback, _
 | 
|             ByVal context As HttpContext, _
 | 
|             ByVal state As [Object])
 | 
|         _callback = callback
 | 
|         _context = context
 | 
|         _state = state
 | 
|         _completed = False
 | 
|     End Sub
 | 
| 
 | 
|     Public Sub StartAsyncWork()
 | 
|         ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf StartAsyncTask), Nothing)
 | 
|     End Sub
 | 
| 
 | 
|     
 | 
| Private Sub StartAsyncTask(ByVal workItemState As [Object])
 | 
| 
 | 
|     Dim request As HttpRequest = _context.Request
 | 
|     Dim response As HttpResponse = _context.Response
 | 
|     Dim connectString As String
 | 
|     Dim sqlCnn As New SqlConnection
 | 
|     Dim sqlCmd As SqlCommand
 | 
|         Dim Sql As String
 | 
| 
 | 
| 
 | 
|     '*** HTTP Header values
 | 
|         Dim Coll As NameValueCollection
 | 
|         '*** Table values
 | 
|         Dim Tx() As String
 | 
| 
 | 
|         ' *** CONNECT ****************************************
 | 
| 
 | 
|         ' Get the ConnectionStrings from POST Form value
 | 
|         Coll = request.Form
 | 
|         connectString = Coll.Get("SQLConnectionString")
 | 
| 
 | 
|         ' Get the ConnectionStrings from Web.config
 | 
|         If connectString = "" Then
 | 
|             ' Get the ConnectionStrings collection. (web.config)
 | 
|             Dim connections As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings
 | 
|             connectString = ConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString
 | 
|         End If
 | 
| 
 | 
|         ' Get the ConnectionStrings Direct
 | 
|         If connectString = "" Then
 | 
|             sqlCnn.ConnectionString = "Server=localhost;Database=master;User ID=sa;Password=xxx"
 | 
|         End If
 | 
| 
 | 
|         ' ***Sql Command *************************************
 | 
| 
 | 
|         ' Get the SQL from POST Form value
 | 
|         Sql = Coll.Get("SQLCommandString")
 | 
| 
 | 
|         ' Get the SQL Direct
 | 
|         If Sql = "" Then
 | 
|            'lists ALL Databases and their MDF-files 
 | 
|             Sql = "SELECT name, filename FROM master..sysdatabases"
 | 
| 
 | 
|             'lists all Tables of a Database 
 | 
|             'Sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"
 | 
| 
 | 
|             'lists all VIEWS of a Database
 | 
|             'Sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW'"
 | 
| 
 | 
|             'lists the column name with the according datatypes of a Table
 | 
|             'Sql = "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SAMPLE'"
 | 
| 
 | 
|         End If
 | 
| 
 | 
|         ' ***Sql EXECUTE ************************************************************************************
 | 
|         sqlCnn.ConnectionString = connectString
 | 
|         Try
 | 
|             sqlCnn.Open()
 | 
|             sqlCmd = New SqlCommand(Sql, sqlCnn)
 | 
|             Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
 | 
| 
 | 
|             ' get numer of fields
 | 
|             Dim fCount As Integer = sqlReader.FieldCount
 | 
|             Dim i As Integer
 | 
| 
 | 
|             ReDim Tx(0)
 | 
| 
 | 
|             While sqlReader.Read()
 | 
|                 If fCount > 1 Then
 | 
|                     For i = 0 To fCount - 2
 | 
|                         Tx(UBound(Tx)) = Tx(UBound(Tx)) & sqlReader.Item(i).ToString & vbTab
 | 
|                     Next i
 | 
|                     Tx(UBound(Tx)) = Tx(UBound(Tx)) & sqlReader.Item(i).ToString
 | 
|                 Else
 | 
|                     Tx(UBound(Tx)) = sqlReader.Item(0).ToString
 | 
|                 End If
 | 
| 
 | 
|                 ReDim Preserve Tx(UBound(Tx) + 1)
 | 
|             End While
 | 
| 
 | 
|             sqlReader.Close()
 | 
|             sqlCmd.Dispose()
 | 
|             sqlCnn.Close()
 | 
| 
 | 
|             For i = 0 To UBound(Tx) - 1
 | 
|                 response.Write(Tx(i) & vbLf)
 | 
|             Next i
 | 
| 
 | 
| 
 | 
|         Catch ex As Exception
 | 
|             response.Write(ex.Message)
 | 
|         End Try
 | 
| 
 | 
| 
 | 
|         _completed = True
 | 
|         _callback(Me)
 | 
|     End Sub 'StartAsyncTask
 | 
| End Class 'AsynchOperation
 | 
|  |