获得局域网中的所有SQL数据库实例
这要使用SQLDMO Com组件
添加引用:
获取局域网中的数据库实例:
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
for (int i = 0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if (srv != null)
{
this.comboxServer.Items.Add(srv);
}
}
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
for (int i = 0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if (srv != null)
{
this.comboxServer.Items.Add(srv);
}
}
读取指定实例的数据库:
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();
try
{
srv.Connect(this.comboxServer.SelectedItem.ToString(), this.txtUser.Text, this.txtPassword.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
foreach (SQLDMO.Database db in srv.Databases)
{
if (db.Name != null)
this.comboxDatabase.Items.Add(db.Name);
}
try
{
srv.Connect(this.comboxServer.SelectedItem.ToString(), this.txtUser.Text, this.txtPassword.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
foreach (SQLDMO.Database db in srv.Databases)
{
if (db.Name != null)
this.comboxDatabase.Items.Add(db.Name);
}