-:Introduction:-
Making generalized database server connection utility.
Steps:
1. Fetch the list of server instances available in LAN.
2. Give user functionality to select particular server instance name from the list of server instances.
3. Fetch the list of databases by connecting with the selected server instance.
4. Give user functionality to give user name and password if there is any to connect with database.
(it is needed only when user selected SQL Authentication mode)
4. Give user functionality to select particular database name from the list.
5. Connect with the database and give user confirmation that successfully connection established.
-:In Detail:-
There are mainly three methods to find the list of servers in local area network:
1. Using SQLDMO class
2. Using SQLSMO class
3. Using general .NET class
Each and every method have their own pros and cons
1. Using SQLDMO class
There are more cons than advantages of use of SQLDMO class
-> It only lists SQL Server 2000 server instances over the LAN.
-> It requires SQLDMO.DLL in specific path C:\Program Files\Microsoft SQL Server\80\Tools\Binn
-> It requires SQLDMO.DLL to be registered under windows server registry through
regsvr32 C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SQLDMO.DLL
command
You cannot use SQLDMO class directly in your application. For using SQLDMO class there is
wrapper provided by microsoft is Interop.SQLDMO.DLL
The wrapper provides you sqldmo wrapper classes to use it inside your application.
You can fetch the list of server instance in LAN by using
Code:
/*
For getting list of server instances name.
*/
//Declaring new object of type SQLServer defined in Interop.SQLDMO.DLL
SQLDMO.SQLServer o_SqlServer;
//Instantiating object o_SqlServer
o_SqlServer = new SQLDMO.SQLServer();
//Declaring namelist object to store list of sql server instances
SQLDMO.NameList o_List;
//Fetching list of sql server instances over the LAN
o_List = o_SqlApplication.ListAvailableSQLServers();
//You can access particular server name by using integer index number.
o_List.Item(index);
/*
Fetch list of databases based on server instance name user have selected
*/
List<String> o_DBList = new List<string>();
o_SqlServer.LoginSecure = true;
if (userName == String.Empty)
{
o_SqlServer.Connect(serverName, null, null);
}
else
{
o_SqlServer.Connect(serverName, userName, password);
}
foreach (Database o_database in o_SqlServer.Databases)
{
o_DBList.Add(o_database.Name);
}
2. Using SQLSMO class
-> It will give you list of both SQL Server 2000 as well as SQL Server 2005
There are some pitfalls of using SQLSMO which prevents us to use this method.
-> It requires four dlls along with the solution or setup to carry, to use this method.
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.SmoEnum.dll
Microsoft.SqlServer.SqlEnum.dll
Code:
/*
For getting list of server instances name.
*/
DataTable serverName;
// get all data sources available in network using SMO
serverName = SmoApplication.EnumAvailableSqlServers();
//To access server names
foreach (DataRow ds in serverName.Rows)
{
string ds_server_name;
ds_server_name = ds[“Name”].ToString();
}
/*
Fetch list of databases based on server instance name user have selected
*/
private Server server = new Server(“selected-server instance-name”); // SMO server object
foreach (Microsoft.SqlServer.Management.Smo.Database db in selected_ds.Databases)
{
//To access database name
String db_name;
db_name = db.Name
}
3. Using general .NET classes
.NET provides some internal classes that comes along with .NET Framework which will give you the
list of server instances.
Once you got server there are some mechanism which you can use to connect with the server and
fetch the list of databases.
Once you got server instance name and database name, it is very easy to connect with that particular
selected database in the selected server instance name.
Code:
/*
For getting list of server instances name.
*/
System.Data.Sql.SqlDataSourceEnumerator servers = System.Data.Sql.SqlDataSourceEnumerator.Instance;
System.Data.DataTable serversTable = servers.GetDataSources();
foreach(DataRow row in serversTable.Rows)
{
//row[0]=networked computer name
//row[1]=database server instance name
//to get full name for connection use following method
string serverinsance_name;
serverinsance_name=row[0].ToString() + @”” + row[1].ToString()
}
/*
Fetch list of databases based on server instance name user have selected
*/
//In generalized method you need to make connection with the selected server instance name
//for fetching list of databases
String ml_conxString;
DataTable o_DatabaseList;
if(string.IsNullOrEmpty(UserName.Text))
{
ml_conxString = “Data Source=” + ServerName.Text.Trim() + “;Integrated Security=True”;
}
else
{
ml_conxString = “Data Source=” + ServerName.Text.Trim() + “;Initial Catalog=” + DataBaseName.Text.Trim() + “;User ID = ” + UserName.Text.Trim() + “;Pwd = ” + Password.Text;
}
using (SqlConnection sqlConx = new SqlConnection(ml_conxString))
{
sqlConx.Open();
o_DatabaseList = sqlConx.GetSchema(“Databases”);
sqlConx.Close();
}
//To access database name
foreach (DataRow row in o_DatabaseList.Rows)
{
string ml_databasename;
ml_databasename = row[“database_name”].ToString();
}
//Testing connection is same as we have fetched database list based on server instance name
String ml_conxString;
if(string.IsNullOrEmpty(UserName.Text))
{
ml_conxString = “Data Source=” + ServerName.Text.Trim() + “;Initial Catalog=” + DataBaseName.Text.Trim() + “;Integrated Security=True”;
}
else
{
ml_conxString = “Data Source=” + ServerName.Text.Trim() + “;Initial Catalog=” + DataBaseName.Text.Trim() + “;User ID = ” + UserName.Text.Trim() + “;Pwd = ” + Password.Text;
}
SqlConnection sqlConx = new SqlConnection(ml_conxString);
sqlConx.Open();
sqlConx.Close();
-:Demo Project (Using General .NET Classes):-
Download GetListOfServers.zip – 4.86 KB
-:Downloads:-
SQLDMO.DLL
Microsoft.SqlServer.ConnectionInfo.DLL
Microsoft.SqlServer.Smo.DLL
Microsoft.SqlServer.SmoEnum.DLL
Microsoft.SqlServer.SqlEnum.DLL