Thursday, July 18, 2013

Code for List of Available databases in Sqlserver

                              This code helps us to know what are databases available in sqlserver. I have created this code in vb.net with sqlserver.  Create a vb form and double click the form design. Copy this code in form load function, Change the data source name(your sqlserver name) and execute it. It gives what are the databases in your sqlserver.


Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Form1
    Dim conn As System.Data.SqlClient.SqlConnection
    Dim str As String
    Dim cmd As System.Data.SqlClient.SqlCommand
    Dim ra As System.Data.SqlClient.SqlDataReader
    Dim da As System.Data.SqlClient.SqlDataAdapter
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New SqlConnection("Data Source=datasource name;Initial Catalog=;Integrated Security=True")
        conn.Open()
        MsgBox("database opened")
        str = "select name from sys.databases"
        cmd = New SqlCommand(str, conn)
        ra = cmd.ExecuteReader
        While ra.Read
            MsgBox("Databses in Sqlserver " & ra(0))

        End While
        ra.Close()
        conn.Close()
    End Sub
End Class


I was tried last one month to get this simple code.

No comments:

Post a Comment

Followers