Mobile Management For SQL Server(s!) - Siccolo - SQL Management Tool
Main     
(II) - How To Develop SQL Mobile Management Tool (and How I created one) - Continued:
Engineered to help you extend your support from mobile devices for Microsoft SQL Servers.

Part IV - Developing SQL Server related management interfaces
Yes, it can be done and it was very easy to develop.
For example, to retrieve database names for a given SQL Server:

Underlying business logic component:
	 Public Function GetDBList(ByVal oLogInf As LoginInfo, _
                                    ByVal ToDebug As Boolean, _
                                    ByRef DBList_XML As String, _
                                    ByRef ErrorInfo As String) As Boolean

            oLoginInfo = oLogInf
            Dim objCommand As SqlCommand = New SqlCommand()
            objCommand = ReturnCommand("sp_databases", ToDebug, ErrorInfo)
            objCommand.CommandType = CommandType.StoredProcedure
            Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
            objDataAdapter.SelectCommand = objCommand
            Dim dsDBList As DataSet = New DataSet("QueryResults")
            objDataAdapter.Fill(dsDBList, "Databases")

            Dim objStringWriter As New System.IO.StringWriter()
            dsDBList.WriteXml(objStringWriter, XmlWriteMode.WriteSchema) 

            DBList_XML = "" & objStringWriter.ToString()

            Return True
        End Function
	
* where oLogInf parameter contains information needed to connect to SQL Server (server address, user name, password)
** ReturnCommand() returns instance of SQLCommand

Corresponding web method:
	  Public Function DBList(ByVal ServerAddress As String, _
                                            ByVal UserName As String, _
                                            ByVal UserPassword As String, _
                                            ByRef ErrorInfo As String) As String

            Try
                With oLoginInfo
                    .sServer = ServerAddress
                    .sLoginName = UserName
                    .sPassword = UserPassword
                    .sDatabase = ""
                End With

                Dim DBList_XML As String


                Dim ToDebugSetting As String = System.Configuration.ConfigurationSettings.AppSettings.Get("DebugMode")
                Dim ToDebug As Boolean = (ToDebugSetting <> "")

                If oCon.GetDBList(oLoginInfo, ToDebug, DBList_XML, ErrorInfo) Then
                    Return DBList_XML
                Else
                    Return ""
                End If

            Catch ex As Exception
                ErrorInfo = ex.Message()
                Return ""
            End Try
        End Function
	


And, on the mobile client (many different ways to present information):
	Friend Function PopulateDBList(ByVal cboListControl As Windows.Forms.ComboBox, _
                                    ByVal AddNoneRow As Boolean, _
                                    ByVal ToSelectDatabase As String, _
                                    ByRef ErrorInfo As String) As Boolean

        Try
            ErrorInfo = ""
            cboListControl.Tag = "...loading..."

            Dim DBList_XML As String
            DBList_XML = objSQLWebServiceManager.DBList(objLoginInfo.ServerAddress, _
                                        objLoginInfo.UserName, _
                                        objLoginInfo.UserPassword, _
                                        ErrorInfo)
            If ErrorInfo <> "" Or DBList_XML = "" Then Return False

            Dim objResponse As New DataSet()
            Dim Reader As New System.IO.StringReader(DBList_XML.ToString())
            Dim XMLReader As New System.Xml.XmlTextReader(Reader)
            XMLReader.Read()

            objResponse.ReadXml(XMLReader, XmlReadMode.Auto)

            If AddNoneRow Then
                Dim dtRowNone As DataRow
                dtRowNone = objResponse.Tables(0).NewRow
                dtRowNone.Item("DATABASE_NAME") = ""
                dtRowNone.Item("DATABASE_SIZE") = 0
                dtRowNone.Item("REMARKS") = ""
                objResponse.Tables(0).Rows.Add(dtRowNone)
            End If

            objResponse.AcceptChanges()

            XMLReader.Close()
            cboListControl.DataSource = objResponse.Tables(0)
            cboListControl.DisplayMember = "DATABASE_NAME"

            If AddNoneRow And ToSelectDatabase = "" Then
                cboListControl.SelectedIndex = cboListControl.Items.Count - 1
            End If

            cboListControl.Tag = ""

            If ToSelectDatabase <> "" Then
                'cboListControl.FindString()
                Dim objSelectedRow As System.Data.DataRowView
                Dim i As Integer = 0

                For i = 0 To cboListControl.Items.Count - 1
                    objSelectedRow = cboListControl.Items(i)

                    If StrComp(ToSelectDatabase, objSelectedRow.Row.ItemArray(0).ToString, CompareMethod.Text) = 0 Then
                        cboListControl.SelectedIndex = i
                        Exit For
                    End If
                Next
            End If

            Return True

        Catch ex As Exception
            ErrorInfo = ex.Message
            If m_LogErrorToFile Then LogError("PopulateDBList():" & ex.ToString)
            Return False
        End Try
    End Function

    Friend Function PopulateDBList(ByVal dataGridControl As Windows.Forms.DataGrid, _
                                    ByRef ErrorInfo As String) As Boolean

        Try
            ErrorInfo = ""

            Dim DBList_XML As String
            DBList_XML = objSQLWebServiceManager.DBList(objLoginInfo.ServerAddress, _
                                        objLoginInfo.UserName, _
                                        objLoginInfo.UserPassword, _
                                        ErrorInfo)
            If ErrorInfo <> "" Or DBList_XML = "" Then Return False

            Dim objResponse As New DataSet()
            Dim Reader As New System.IO.StringReader(DBList_XML.ToString())
            Dim XMLReader As New System.Xml.XmlTextReader(Reader)
            XMLReader.Read()

            objResponse.ReadXml(XMLReader, XmlReadMode.Auto)

            objResponse.AcceptChanges()

            XMLReader.Close()
            dataGridControl.DataSource = objResponse.Tables(0)

            Return True

        Catch ex As Exception
            ErrorInfo = ex.Message
            If m_LogErrorToFile Then LogError("PopulateDBList():" & ex.ToString)
            Return False
        End Try
    End Function

    Friend Function PopulateDBList(ByVal databasesNode As Windows.Forms.TreeNode, _
                                   ByRef ErrorInfo As String) As Boolean

        Try
            ErrorInfo = ""

            Dim DBList_XML As String
            DBList_XML = objSQLWebServiceManager.DBList(objLoginInfo.ServerAddress, _
                                        objLoginInfo.UserName, _
                                        objLoginInfo.UserPassword, _
                                        ErrorInfo)
            If ErrorInfo <> "" Or DBList_XML = "" Then Return False

            Dim objResponse As New DataSet()
            Dim Reader As New System.IO.StringReader(DBList_XML.ToString())
            Dim XMLReader As New System.Xml.XmlTextReader(Reader)
            XMLReader.Read()

            objResponse.ReadXml(XMLReader, XmlReadMode.Auto)

            objResponse.AcceptChanges()

            XMLReader.Close()

            Dim newNode As Windows.Forms.TreeNode = New Windows.Forms.TreeNode()

            If (databasesNode.Nodes.Count > 0) Then 'Check we have already listed the DB's.
                databasesNode.Nodes.Clear()	'  If it so then remove it and rebuild it	
            End If

            Dim objDataRow As DataRow
            Dim strNode As String = ""

            For Each objDataRow In objResponse.Tables("Databases").Rows
                strNode = objDataRow("DATABASE_NAME").ToString()
                newNode = databasesNode.Nodes.Add(strNode)
                newNode.Tag = "database"
            Next

            Return True

        Catch ex As Exception
            ErrorInfo = ex.Message
            If m_LogErrorToFile Then LogError("PopulateDBList():" & ex.ToString)
            Return False
        End Try
    End Function
	


Consequently, to retrieve table names within a database (same for views, and for stored procedures use dbo.sp_stored_procedures system stored procedure):
	Public Function GetTableList(ByVal oLogInf As LoginInfo, _
                                    ByVal DatabaseName As String, _
                                    ByVal ToDebug As Boolean, _
                                    ByRef TableList_XML As String, _
                                    ByRef ErrorInfo As String) As Boolean

            oLoginInfo = oLogInf
            Dim objCommand As SqlCommand = New SqlCommand()
            objCommand = ReturnCommand(DatabaseName & ".dbo.sp_tables", ToDebug, ErrorInfo)
            objCommand.CommandType = CommandType.StoredProcedure

            Dim objParm As SqlParameter = objCommand.Parameters.Add("@table_type", SqlDbType.VarChar, 100)
            objParm.Value = "'TABLE'"

            Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
            objDataAdapter.SelectCommand = objCommand

            Dim objTableList As DataSet = New DataSet("QueryResults")
            objDataAdapter.Fill(objTableList, "Tables")

            Dim objStringWriter As New System.IO.StringWriter()
            objTableList.WriteXml(objStringWriter, XmlWriteMode.WriteSchema)   ', XmlWriteMode.IgnoreSchema)

            TableList_XML = "" & objStringWriter.ToString()

            Return True
        End Function
	


And, on the mobile client (many different ways to present information):
	Friend Function PopulateTableList(ByVal cboListControl As Windows.Forms.ComboBox, _
                                   ByVal SelectedDatabase As String, _
                                   ByRef ErrorInfo As String) As Boolean

        Try
            ErrorInfo = ""

            Dim TableList_XML As String
            TableList_XML = objSQLWebServiceManager.TableList(objLoginInfo.ServerAddress, _
                                        objLoginInfo.UserName, _
                                        objLoginInfo.UserPassword, _
                                        SelectedDatabase, _
                                        ErrorInfo)
            If ErrorInfo <> "" Or TableList_XML = "" Then Return False

            Dim objResponse As New DataSet()
            Dim Reader As New System.IO.StringReader(TableList_XML.ToString())

            Dim XMLReader As New System.Xml.XmlTextReader(Reader)
            XMLReader.Read()

            objResponse.ReadXml(XMLReader, XmlReadMode.Auto)

            objResponse.AcceptChanges()

            XMLReader.Close()
            cboListControl.DataSource = objResponse.Tables(0)
            cboListControl.DisplayMember = "TABLE_NAME"

            Return True

        Catch ex As Exception
            ErrorInfo = ex.Message
            If m_LogErrorToFile Then LogError("PopulateTableList():" & ex.ToString)
            Return False
        End Try
    End Function

    Friend Function PopulateTableList(ByVal SelectedDatabase As String, _
                                        ByVal tablesNode As Windows.Forms.TreeNode, _
                                      ByRef ErrorInfo As String) As Boolean

        Try
            ErrorInfo = ""

            Dim TableList_XML As String
            TableList_XML = objSQLWebServiceManager.TableList(objLoginInfo.ServerAddress, _
                                        objLoginInfo.UserName, _
                                        objLoginInfo.UserPassword, _
                                        SelectedDatabase, _
                                        ErrorInfo)

            If ErrorInfo <> "" Or TableList_XML = "" Then Return False

            Dim objResponse As New DataSet()
            Dim Reader As New System.IO.StringReader(TableList_XML.ToString())

            Dim XMLReader As New System.Xml.XmlTextReader(Reader)
            XMLReader.Read()

            objResponse.ReadXml(XMLReader, XmlReadMode.Auto)

            objResponse.AcceptChanges()

            XMLReader.Close()

            Dim newNode As Windows.Forms.TreeNode = New Windows.Forms.TreeNode()

            If (tablesNode.Nodes.Count > 0) Then 'Check we have already listed the tables... 
                tablesNode.Nodes.Clear()	' If it so then remove it and rebuild it
            End If

            Dim objDataRow As DataRow
            Dim strNode As String = ""

            For Each objDataRow In objResponse.Tables("Tables").Rows
                strNode = objDataRow("TABLE_NAME").ToString()
                newNode = tablesNode.Nodes.Add(strNode)
                newNode.Tag = "table"
            Next

            Return True

        Catch ex As Exception
            ErrorInfo = ex.Message
            If m_LogErrorToFile Then LogError("PopulateTableList():" & ex.ToString)
            Return False
        End Try
    End Function	
	


To execute SQL:
	Public Function ExecuteSQLCommand(ByVal oLogInf As LoginInfo, _
                                        ByVal SQLCommand As String, _
                                        ByVal ToDebug As Boolean, _
                                        ByRef SQLResult_XML As String, _
                                        ByRef RowCount As Integer, _
                                        ByRef ErrorInfo As String) As Boolean
            Try

                oLoginInfo = oLogInf

                Dim objDataSet As New DataSet()
                If ReturnDataset(SQLCommand, ToDebug, objDataSet, RowCount, ErrorInfo) Then

                    Dim objStringWriter As New System.IO.StringWriter()
                    objDataSet.WriteXml(objStringWriter, XmlWriteMode.WriteSchema)   ', XmlWriteMode.IgnoreSchema)

                    SQLResult_XML = "" & objStringWriter.ToString()

                    Return True
                Else

                    Return False
                End If

            Catch ex_execute As Exception
                ErrorInfo = ex_execute.Message
                Return False
            End Try
        End Function
	
* where ReturnDataset() creates DataAdapter, DataSet, fills DataSet and returns RowCount

On the client side:
	Friend Function ExecuteSQL(ByVal strSQL As String, _
                                   ByVal dataGridControl As Windows.Forms.DataGrid, _
                                   ByRef intRowCount As Integer, _
                                   ByRef ErrorInfo As String) As Boolean

        Try
            ErrorInfo = ""
            Dim SQLResult_XML As String
            SQLResult_XML = objSQLWebServiceManager.ExecuteSQL(objLoginInfo.ServerAddress, _
                                                    objLoginInfo.UserName, _
                                                    objLoginInfo.UserPassword, _
                                                    strSQL, _
                                                    intRowCount, _
                                                    ErrorInfo)

            If ErrorInfo <> "" Or SQLResult_XML = "" Then Return False

            Dim objResponse As New DataSet()
            Dim Reader As New System.IO.StringReader(SQLResult_XML.ToString())
            Dim XMLReader As New System.Xml.XmlTextReader(Reader)
            XMLReader.Read()

            Try
                objResponse.ReadXml(XMLReader, XmlReadMode.Auto)
                objResponse.AcceptChanges()
                dataGridControl.DataSource = objResponse.Tables(0)
            Catch
                dataGridControl.DataSource = Nothing
            End Try


            XMLReader.Close()

            Return True

        Catch ex As Exception
            ErrorInfo = ex.Message
            If m_LogErrorToFile Then LogError("ExecuteSQL():" & ex.ToString)
            Return False
        End Try
    End Function
	

To make mobile interface more friendlier for the use, on the mobile device I added short-cuts:
Mobile Management For SQL Server(s!) - Free! - Siccolo - Free SQL Management Tool

By the way, to get table name out of drop-down box:
	Private Function GetSelectedTable() As String
		If cboTableList.Items.Count = 0 Then Return ""
		Dim strSelectedTable As String
		Dim objSelectedRow As System.Data.DataRowView
		objSelectedRow = (cboTableList.SelectedItem)
		strSelectedTable = objSelectedRow.Row.ItemArray(2).ToString
		Return "[" & strSelectedTable & "]"
   	End Function
	

Side Note:
CompareMethod - When you call comparison functions, you can use the CompareMethod enumeration in your code in place of the actual values. CompareMethod.Binary is for a binary comparison, and CompareMethod.Text performs a textual comparison (i.e. non-casesensitive)
Back to (I)      Continue to (III)




Free Mobile Management For SQL Server(s!) - Siccolo - SQL Management ToolQuestions? Suggestions? Concerns? - email me to siccolo_mobile_management@yahoo.com    Greg Dubinovsky © 2006-2007
or share your thoughts at Siccolo Blog

Web being sponsor - Mid-Atlantic Processing. Well being sponsor - Clarity MediSpa. Hairless sponsor - Clarity MediSpa Laser Hair Removal.