Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
' Get the connection string named "DB" from "web.config" file.
' Replace the "DB" with the name of the your datyabase connection.
Private strConnectionString As String = _
ConfigurationManager.ConnectionStrings("DB").ConnectionString
' Declare shared connection var. The connection must remain the same for all the callbacks.
Shared conn As SqlConnection = Nothing
Shared cmd As SqlCommand
' ASP.NET 2.0 callbacks related stuff. Google with "ASP.NET 2.0 callbacks" to find out how it works.
Private cs As ClientScriptManager = Page.ClientScript
Private _callbackArg As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CallbackRef As String = _
Page.ClientScript.GetCallbackEventReference(Me, "arg", "ProcessCallBack", "context")
End Sub
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements ICallbackEventHandler.RaiseCallbackEvent
_callbackArg = eventArgument
End Sub
Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
'
Dim strCallbackCommand As String = Left(_callbackArg, 2)
Dim strCallbackCommandText As String = Mid(_callbackArg, 3)
Select Case strCallbackCommand
Case Is = "01" ' Open the connection
Dim strCallbackMsg As String = "11 Connection opened; need the same shared connection through all the requests of the batch; we are creating the database, not using it."
Try
conn = New SqlConnection(strConnectionString)
conn.Open()
cmd = conn.CreateCommand
Catch ex As Exception
strCallbackMsg = String.Format("21 Could not open the connection. Error was {0}", ex.ToString())
End Try
Return strCallbackMsg
Case Is = "02" ' Close out the connection
If (conn IsNot Nothing) Then
Try
conn.Close()
conn.Dispose()
Catch ex As Exception
Me.Response.Write(String.Format("Could not close the connection. Error was {0}", ex.ToString()))
End Try
End If
Return "12 Connection closed"
Case Is = "03" ' Execute T-SQL against the target database
cmd.CommandText = strCallbackCommandText
cmd.CommandTimeout = 600
Try
cmd.ExecuteNonQuery()
Catch e As SqlException
' Handle exception.
Return "14 " & e.Message
Finally
End Try
Return "13 Succeded"
Case Else
Return "00_Unrecognized command"
End Select
End Function
End Class
07 September, 2009
Deploying a SQL Database to a Remote Host by using ASP.NET 2.0 callbacks (vb code behind)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment