.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Loading all database into combo box and loading all sqlsever name in C# .Net

Posted By:Rajshree       Posted Date: October 25, 2010    Points: 15    Category: Visual Studio    URL: http://www.dotnetspark.com  

Loading database into combobox as well server name that are available...
 

Import reference:

//This is available in sql sever SDK Folder

Microsoft.SqlServer.ConnectionInfo

Microsoft.SqlServer.Smo

//Use namespace to import.

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;

Declare Object of Server Class

Code in load event of form:

        //datatable object

 

        DataTable dt = SmoApplication.EnumAvailableSqlServers(true);

        if (dt.Rows.Count > 0)

        {

            // Loop through each server in the DataTable

            foreach (DataRow dr in dt.Rows)

            {

 

                //cmbsrv_name is name of combo box in //which all server name will be loaded..

                cmbsrv_name.Items.Add(dr["Name"]);

            }

        }

To load all database code:

This code will be in button click event to connect with server.

//cmbsrv_name is name of combo in which all server name loaded.

 

        if (cmbsrv_name.SelectedItem != null && cmbsrv_name.SelectedItem.ToString() != "")

        {

            ServerConnection srvcon = newServerConnection(cmbsrv_name.SelectedItem.ToString());

            srvcon.LoginSecure = false;

 

            //txtuserid is id of server and txtpassword password of server to connect

            srvcon.Login = txtuserid.Text;

            srvcon.Password = txtPassword.Text;

            srv = new Server(srvcon);

            foreach (Database db in srv.Databases)

            {

                //cmbdatabase is name of combobox in which all database related to that server will be loaded

 

                cmbDatabase.Items.Add(db.Name);

            }

        }

Thanks you



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend