Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Retrieving all databases Name from database server using SMO namespace in C# and VB using .NET 2.0.

Author: Zunnair
Download Source Code : 774_CreateDataBase.zip

In this simple article you will learn that how to get Retrieve all databases Name from database server using SMO namespace

Simply just create New Project of windows application in C#.

 

Drag 2 buttons, 1 Combo box , 3 labels and 3 TextBoxes on the form.

 

Add two references of namespaces

 

Microsoft.SqlServer.Smo

Microsoft.SqlServer.ConnectionInfo

 

Now write following code on form Load

C#


 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Database Getting APP";

            textBox1.Text = "DBServer";

            textBox2.Text = "UserName";

            textBox3.Text = "Password";

        }



VB


 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DevAsp Database Getting APP"

        textBox1.Text = "DBServer"

        textBox2.Text = "UserName"

        textBox3.Text = "Password"

 

    End Sub



Now write code on button click events


C#

 

private void button1_Click(object sender, EventArgs e)

        {

            ServerConnection srvConn = new ServerConnection(textBox1.Text);

           

            srvConn.LoginSecure = false;

            srvConn.Login = textBox2.Text;

            srvConn.Password = textBox3.Text;

            srvSql = new Server(srvConn);

            button2.Enabled = true;

        }

 

     

 

        private void button2_Click(object sender, EventArgs e)

        {

            comboBox1.Items.Clear();

            foreach (Database dbServer in srvSql.Databases)

            {

                // Add database to combobox

                comboBox1.Items.Add(dbServer.Name);

              

            }

          

        }



VB


 

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim srvConn As ServerConnection = New ServerConnection(textBox1.Text)

 

        srvConn.LoginSecure = False

        srvConn.Login = textBox2.Text

        srvConn.Password = textBox3.Text

        srvSql = New Server(srvConn)

        button2.Enabled = True

    End Sub

 

 

 

    Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)

        comboBox1.Items.Clear()

        Dim dbServer As Database

        For Each dbServer In srvSql.Databases

            ' Add database to combobox

            comboBox1.Items.Add(dbServer.Name)

 

        Next

 

    End Sub







Article Comments
hi.tnx for your good artile.
i used this program.but when i enter a incorrect username and password i have this error:
Failed to connect to server SERVER-.
how can i fix that?
tnx

Posted on 11/29/2010 2:38:51 AM by mahboobeh

Hi

when you provide user and password then it connect to your server otherwise how can application access that....

Thanks.

Posted on 12/2/2010 11:14:13 AM by Zunair

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Automated Adjustable ComboBox Using C#

Disclaimer - Privacy
© 2002-2017 DevASP.net